Skip to content

Instantly share code, notes, and snippets.

@emilhein
Last active July 8, 2018 14:47
Show Gist options
  • Save emilhein/8d7820a97ec331304be9bed1ac1a3331 to your computer and use it in GitHub Desktop.
Save emilhein/8d7820a97ec331304be9bed1ac1a3331 to your computer and use it in GitHub Desktop.
es6 wait with asyn to "throotle" promises
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