javascriptnode.jsco

Parallelize execution of Promise containing co wrapped generator


I'm creating seed data for my database. The seed function is co-wrapped generator (https://www.npmjs.com/package/co):

createSeedData () {
  co(function * () {
     [...]
  }
}

At one point I'm trying to create 40 auctions with one bid each. Up until now I had a for-loop like this:

for (let i = 0; i <= 40; i++) {
  let auction = yield auction.create({
     [...]
  })  // Async creation of model and persisting it in db 
  yield bid.create({
    auction: auction.id
    [...]
  })
}

Now I'd like to create the auction in parallel but I can't quite get it to work.

co supports parallel execution when yield is used with arrays.

However I still need to create first an invoice, then a bid for it using yield.

I tried wrapping the above in a Promise and an inner co-wrapped generator function but it just never finishes execution.

How can I solve this problem? (If possible without using more packages)

Here is my attempt with the inner generator:

let promises = []
for (let i = 0; i <= 40; i++) {
  promises.push(new Promise(
    co(function *(resolve, reject) {
      let auction = yield auction.create({
        [...]
      })  // Async creation of model and persisting it in db

      yield bid.create({
        auction: auction.id
        [...]
      })
    )
    resolve()
  ))
}
yield invoicePromises

Solution

  • There's no need to call new Promise (and it doesn't work because you never call resolve or reject). Just omit that part, co() already returns a promise:

    let promises = [];
    for (let i = 0; i <= 40; i++) {
      promises.push( co(function *() {
        let auction = yield auction.create({
          [...]
        });  // Async creation of model and persisting it in db
        yield bid.create({
          auction: auction.id
          [...]
        });
      ));
    }
    yield invoicePromises;
    

    If you don't want to use an inner generator, you can do the chaining with plain promises as well:

    let promises = [];
    for (let i = 0; i <= 40; i++) {
      promises.push(
        auction.create({
          [...]
        }).then(auction =>
          bid.create({
            auction: auction.id
            [...]
          });
        )
      );
    }
    yield Promise.all(invoicePromises);