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