Files
30-seconds-of-code/snippets/chain-async-functions.md
Angelos Chalaris e9d912c2ac Update chain-async-functions.md
Improved example and formatting
2017-12-13 12:12:12 +02:00

453 B

Chain 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');  setTimeout(next, 1000); },
  next => { console.log('2 seconds');}
])
*/