Created
April 28, 2021 06:57
-
-
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
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
| // 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