Last active
January 15, 2019 16:25
-
-
Save indatawetrust/5e535d2072d8fb2911159acb2893ab71 to your computer and use it in GitHub Desktop.
async await waterfall sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const waterfall = async function (input, ...funcs) { | |
const list = [] | |
for(let f of funcs) | |
if(!list.length) | |
list.push(await f(input)) | |
else | |
list.push(await f(list.shift()) | |
return list.pop() | |
} | |
const upper = (_) => { | |
return new Promise(res => { | |
res(_.toUpperCase()) | |
}) | |
} | |
const reverse = (_) => { | |
return new Promise(res => { | |
res(_.split('').reverse().join('')) | |
}) | |
} | |
const space = (_) => { | |
return new Promise(res => { | |
res(_.replace(/ {0,}/g,' ')) | |
}) | |
} | |
waterfall("tpircsavaj", upper, reverse, space).then(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really cool, thanks!