Last active
October 23, 2022 08:11
-
-
Save styopdev/1afdad46b92997d155e0106ccf1549ed to your computer and use it in GitHub Desktop.
print range of numbers promises
This file contains hidden or 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
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