How to await inside RxJS subscribe method How to await inside RxJS subscribe method typescript typescript

How to await inside RxJS subscribe method


you can just directly add async signature to the anonymous function call in subscribe

 this.subscriber = dateSubscription.subscribe(async (date: Date) => {    let dbKey = await this._someService.saveToDatabase(someObject);    // wait for db write to finish before evaluating the next code    // ... some other code here  });


You do not need to use await, nor need to convert your Promise to an Observable.


CF this Tweet from Ben Lesh :

enter image description here


Here's an example with a mock for the function saveToDatabase :
(and the working Plunkr : https://plnkr.co/edit/7SDLvRS2aTw9gYWdIznS?p=preview)

const { Observable } = Rx;const saveToDatabase = (date) =>  new Promise(resolve =>    setTimeout(() =>      resolve(`${date} has been saved to the database`),      1000));const date$ = Observable.of(new Date()).delay(1000);date$  .do(x => console.log(`date received, trying to save it to database ...`))  .switchMap(date => saveToDatabase(date))  .do(console.log)  .subscribe();

Output :
enter image description here


Update:Found easy for one time promises just use toPromise in front of the subscriber (blog). So for the above case it will be like this:

const title = await this.translate.get('MYBOOK-PAGE.PAGE_TITLE').toPromise();

Old way:Here's my method of solving this issue

const title = await new Promise<string>(resolve =>   this.translate.get('MYBOOK-PAGE.PAGE_TITLE')   .subscribe(translated => {     resolve(translated)   }));

Here what I'm doing is changing my Observable to a Promise

Note: Here the only problem is this is one time show ie. if you subscribe once you won't able to access it again. Suited me so sharing here.