I'm applying an async method to each value emitted by an observalbe. I would like this async method to be applied to the next emitted value only once the async method completed for the previous value.
Here is a short example :
import { of } from "rxjs"; const timeout = (ms) => { return new Promise(resolve => setTimeout(resolve, ms)); }; of(1, 2, 3).subscribe(async (val) => { console.log(`starting to use ${val}`); await timeout(1000); console.log(`done using ${val}`); }); timeout is updating my state and fetching data from a server
Output :
// starting to use 1 // starting to use 2 // starting to use 3 (wait 1 sec) // done using 1 // done using 2 // done using 3 What I would lie to get is :
// starting to use 1 (wait 1 sec) // done using 1 // starting to use 2 (wait 1 sec) // done using 2 // starting to use 3 (wait 1 sec) // done using 3