How to convert promise to observable in rxjs

Note: Not complete yet..
The woocommerce API npm is promise-based. To convert promise to observable I did

  getProducts(page: number): Observable<any> {
    return new Observable((observer) => {
      this.wooCommerce.getAsync('products').then((data) => {
        observer.next(JSON.parse(data.body));
        observer.complete();
      }).catch((error) => {
        console.error(error);
        observer.error();
      });

    });
  }
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top