Skip to content

Instantly share code, notes, and snippets.

@Namchee
Created April 28, 2021 06:57
Show Gist options
  • Save Namchee/a1b00d34d1663e016163b96e578ad521 to your computer and use it in GitHub Desktop.
Save Namchee/a1b00d34d1663e016163b96e578ad521 to your computer and use it in GitHub Desktop.
Serial Promise Resolver - courtesy of Anthony Budianto's tweet https://twitter.com/antonybudianto/status/1387211508296261633
// really quick and dirty way to solve it.
let count = 0;
const promisified = (time) => new Promise(resolve => {
setTimeout(resolve, time);
});
async function timeoutProms(proms, c, time) {
count++;
await promisified(time);
console.log(time);
count--;
serialCon(proms, c);
}
function serialCon(proms, c) {
if (count < c && proms.length) {
timeoutProms(proms, c, proms[0]);
proms.shift();
serialCon(proms, c);
} else {
return;
}
}
const arr = [5000, 1000, 100, 300, 2000];
serialCon(arr, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment