Last active
July 8, 2018 14:47
-
-
Save emilhein/8d7820a97ec331304be9bed1ac1a3331 to your computer and use it in GitHub Desktop.
es6 wait with asyn to "throotle" promises
This file contains 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 timeout = ms => new Promise(res => setTimeout(res, ms)) | |
async function delayFunc(delay, func, input) { | |
await timeout(delay) | |
return func(input) | |
} | |
let myPromise = input => { | |
return new Promise((resolve, reject) => { | |
console.log('HI ', input) | |
resolve(input) | |
}) | |
} | |
let allProm = [] | |
let arr = [1, 2,3 ] | |
arr.forEach(ele => { | |
let theWait = ele * 1000 *2 | |
allProm.push(delayFunc(theWait, myPromise, 'You')) | |
}) | |
Promise.all(allProm) | |
.then(res => { | |
console.log(res); | |
}) | |
.catch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment