diff --git a/README.md b/README.md index 7308593a0..6c1f8c196 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ * [Average of array of numbers](#average-of-array-of-numbers) * [Capitalize first letter of every word](#capitalize-first-letter-of-every-word) * [Capitalize first letter](#capitalize-first-letter) +* [Chain asynchronous functions](#chain-asynchronous-functions) * [Check for palindrome](#check-for-palindrome) * [Chunk array](#chunk-array) * [Count occurrences of a value in array](#count-occurrences-of-a-value-in-array) @@ -110,6 +111,21 @@ const capitalize = (str, lowerRest = false) => // capitalize('myName', true) -> 'Myname' ``` +### 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('0 seconds'); setTimeout(next, 1000); }, + next => { console.log('1 second'); setTimeout(next, 1000); }, + next => { console.log('2 seconds'); } +]) +*/ +``` + ### Check for palindrome Convert string `toLowerCase()` and use `replace()` to remove non-alphanumeric characters from it. diff --git a/snippets/chain-async-functions.md b/snippets/chain-asynchronous-functions.md similarity index 90% rename from snippets/chain-async-functions.md rename to snippets/chain-asynchronous-functions.md index 5962f29ff..6c6118b3c 100644 --- a/snippets/chain-async-functions.md +++ b/snippets/chain-asynchronous-functions.md @@ -4,11 +4,11 @@ Loop through an array of functions containing asynchronous events, calling `next ```js 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');} + next => { console.log('2 seconds'); } ]) */ ```