Skip to content

Instantly share code, notes, and snippets.

@Restuta
Last active December 28, 2022 19:43
Show Gist options
  • Save Restuta/ee4cc5f3f7d9e6cc485798228326a9da to your computer and use it in GitHub Desktop.
Save Restuta/ee4cc5f3f7d9e6cc485798228326a9da to your computer and use it in GitHub Desktop.
run promises sequentially or not using observables
import * as O from 'rxjs';
const delay = ms => new Promise((resolve) => setTimeout(() => resolve(ms), ms));
O.from([1,2,3])
.pipe(
O.map(x => O.defer(() => delay(1000))),
O.mergeAll(1)
).subscribe(x => console.log(x))
console.log('done')
// allows grouiping / batching by count
import * as O from 'rxjs';
console.log('hi') //?
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
O.from([1,2,3, 4, 5, 6, 7, 8, 9 , 10])
.pipe(
O.bufferCount(3),
O.map(x => O.defer(() => delay(1000, x))),
O.mergeAll(1)
).subscribe(x => console.log(x))
console.log('done')
@Restuta
Copy link
Author

Restuta commented Dec 28, 2022

This will execute promises with concurrency passed to mergeAll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment