0.0.0 • Published 8 years ago

@freebroccolo/promise-bag v0.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

@freebroccolo/promise-bag

Travis Build Status dependency Status devDependency Status

What is this?

A "bag" of promises which implements the Generator interface. You put the promises in and then observe them in the order in which they resolve. You can think of this as implementing an iterative Promise.race which races a bunch of promises, removes the winner from the pool, and continues racing the remaining promises until the pool is empty.

Example:

async function main() {
  const tasks = new PromiseBag();
  for (let i = 0; i < 10; i++) {
    tasks.put(new Promise((resolve) => setTimeout(() => resolve(i), Math.random() * 100)));
  }
  for (const val of tasks) {
    console.log(await val);
  }
}

Output:

0
4
7
6
8
1
9
5
2
3