Skip to content

Instantly share code, notes, and snippets.

@getify getify/1.js

Last active Jun 8, 2019
What would you like to do?
async function racePromisesPool([ ...prs ] = []) {
var raceWon = false;
var prListeners = listen(pr,idx){
return pr.then(function t(v){
if (!raceWon) {
raceWon = true;
prs.splice(idx,1); // remove the promise from the pool since it won the race
return v;
return [ prs, await Promise.race(prListeners) ];
// *******************************************
async function main() {
// pull file responses ASAP and print them out as they arrive
for await (let text of loadFiles([
])) {
async function *loadFiles(files) {
var pool =;
while (pool.length > 0) {
// get the next-ready promise from the pool
let nextResponse;
[ pool, nextResponse ] = await racePromisesPool(pool);
// push response out through iterator
yield nextResponse;
function getFile(file) {
return new Promise(function c(resolve){

This comment has been minimized.

Copy link

bcdbuddy commented Jun 7, 2019

Hello @getify,
How useful this could be in a project ? I still don't see any use case for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.