Created
February 4, 2024 07:30
-
-
Save smigniot/551c972d663410fc52c6c928adc17bb0 to your computer and use it in GitHub Desktop.
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
| async function run(elements) { | |
| // ============ | |
| // your code here starts here | |
| const results = []; | |
| const pending = new Set(); | |
| const order = elements.map((element,i)=>({element,i})); | |
| while(order.length) { | |
| const {element,i} = order.shift(); | |
| const promise = api(element); | |
| pending.add(promise); | |
| promise.then((result)=>{ | |
| pending.delete(promise); | |
| results[i] = result; | |
| }); | |
| if(pending.size >= MAX_INFLIGHT) { | |
| await Promise.any(pending); | |
| } | |
| } | |
| await Promise.all(pending); | |
| return results; | |
| // your code ends here | |
| // ============ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment