Last active
November 17, 2017 11:32
-
-
Save q-ode/dc1fb4e0d63de9794d8ed39f11853915 to your computer and use it in GitHub Desktop.
Sequential Promise Execution
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 arrayOfSlackMessages = [1, 2, 3, 4, 5]; | |
const waitTimes = [1000, 2000, 5000, 10000]; | |
function sendSlackMessage(message, waitTime) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
resolve(); | |
}, waitTime); | |
}) | |
} | |
async function execute () { | |
for (let message of arrayOfSlackMessages) { | |
const waitTime = waitTimes[Math.floor(waitTimes.length * Math.random())]; | |
console.log(`Running: ${message}, Wait time:${waitTime}`); | |
await sendSlackMessage(message, waitTime); | |
console.log(`${message} done.`); | |
} | |
} | |
execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment