click-async
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

click-async

npm version

Are you tired of subscribing and unsubscribing when calling a service that does some asynchronous action on click? Use the clickAsync!

How does it work

Let us imagine having a function in the component which you call on button click:

save(product: Product) {
    this.apiService.saveProduct(product)
        .pipe(
            takeUntil(this.onDestroy)
        ).subscribe(
            (response) => console.log('yaaay product saved')
        );
}

Remember to unsusbribe!


Now imagine a much more readable version of the function, which simply returns the Observable:

save(product: Product): Observable<Product> {
    return this.apiService.saveProduct(product).pipe(
        tap((response) => console.log('yaaay product saved'))
    );
}

Now the template. To call the function use the clickAsync output, available for you via the ClickAsyncDirective.

<button [clickAsync]="save(product)">Save</button>

Lastly, don't forget to import the directive to your module:

@NgModule({
  imports: [ClickAsyncDirective],
})
export class SomeModule {}

Enjoy!

Installation

npm install --save click-async

Readme

Keywords

none

Package Sidebar

Install

npm i click-async

Weekly Downloads

0

Version

0.0.3

License

none

Unpacked Size

13.5 kB

Total Files

11

Last publish

Collaborators

  • maciejwojcik