Skip to content

Instantly share code, notes, and snippets.

@q-ode
Last active November 17, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save q-ode/dc1fb4e0d63de9794d8ed39f11853915 to your computer and use it in GitHub Desktop.
Save q-ode/dc1fb4e0d63de9794d8ed39f11853915 to your computer and use it in GitHub Desktop.
Sequential Promise Execution
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