Execute Promise calls sequentially with/without a library

Recently I had a chance to write code to sequentially execute Promise calls. Here’s a record for it.

With Bluebird.js

await Promise.mapSeries(items, (item: string) => {
  return new Promise((resolve) =>
    setTimeout(() => {
      console.log(`item: ${item}`);
    }, 1000)

Without any library

async function processItemsSeq(items: string[]): Promise<void> {
  return items.reduce((acc, item) => {
    return acc.then(() => console.log(`item: ${item}`));
  }, Promise.resolve());

Leave a Reply

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