Files
30-seconds-of-code/snippets/chainAsync.md
Angelos Chalaris 8281457945 Updated examples
Removed duplicate and unnecessary examples.
2018-01-04 14:22:56 +02:00

430 B

chainAsync

Chains asynchronous functions.

Loop through an array of functions containing asynchronous events, calling next when each asynchronous event has completed.

const chainAsync = fns => {
  let curr = 0;
  const next = () => fns[curr++](next);
  next();
};
chainAsync([
  next => {
    console.log('0 seconds');
    setTimeout(next, 1000);
  },
  next => {
    console.log('1 second');
  }
]);