From a1a5fb291b158a00602c834a4328f3d806237314 Mon Sep 17 00:00:00 2001 From: Robin Thomas <> Date: Tue, 12 Dec 2017 10:24:42 -0600 Subject: [PATCH 1/2] Added async function chain --- snippets/chain-async-functions.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/chain-async-functions.md diff --git a/snippets/chain-async-functions.md b/snippets/chain-async-functions.md new file mode 100644 index 00000000000..528fcb1a698 --- /dev/null +++ b/snippets/chain-async-functions.md @@ -0,0 +1,15 @@ +### Chain asynchronous functions + +Loop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed. + +```js +const chainAsync = fns => { + let curr = 0; const next = () => fns[curr++](next); next() +} +chainAsync([ + next => { console.log('This happens at 0 seconds'); setTimeout(next, 1000) }, + next => { console.log('This happens at 1 second'); setTimeout(next, 1000) }, + next => { console.log('This happens at 2 seconds'); setTimeout(next, 1000) }, + next => { console.log('Done at 3 seconds!') } +]) +``` From e9d912c2ac73b5de366706a9f2f65c835c99ad38 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 13 Dec 2017 12:12:12 +0200 Subject: [PATCH 2/2] Update chain-async-functions.md Improved example and formatting --- snippets/chain-async-functions.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/snippets/chain-async-functions.md b/snippets/chain-async-functions.md index 528fcb1a698..5962f29ffd5 100644 --- a/snippets/chain-async-functions.md +++ b/snippets/chain-async-functions.md @@ -3,13 +3,12 @@ Loop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed. ```js -const chainAsync = fns => { - let curr = 0; const next = () => fns[curr++](next); next() -} +const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); } +/* chainAsync([ - next => { console.log('This happens at 0 seconds'); setTimeout(next, 1000) }, - next => { console.log('This happens at 1 second'); setTimeout(next, 1000) }, - next => { console.log('This happens at 2 seconds'); setTimeout(next, 1000) }, - next => { console.log('Done at 3 seconds!') } + next => { console.log('0 seconds'); setTimeout(next, 1000); }, + next => { console.log('1 second'); setTimeout(next, 1000); }, + next => { console.log('2 seconds');} ]) +*/ ```