Skip to content

Instantly share code, notes, and snippets.

@styopdev
Last active October 23, 2022 08:11
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 styopdev/1afdad46b92997d155e0106ccf1549ed to your computer and use it in GitHub Desktop.
Save styopdev/1afdad46b92997d155e0106ccf1549ed to your computer and use it in GitHub Desktop.
print range of numbers promises
function printNumbers(a, b) {
if (a > b || typeof a !== 'number' || typeof b !== 'number') {
throw 'Incorrect arguments';
}
for(let i = a, counter = 0, promises = []; i <= b; i++, counter++) {
promises.push(
new Promise((resolve, reject) => {
setTimeout(() => {
resolve(i)
}, 1000 * counter)
}
));
if (i == b) {
return promises;
}
}
}
printNumbers(1, 5)
.forEach(p => p.then(console.log));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment