AD95
AD95

Reputation: 180

can we run async() parallel using multi threading?

here in loop I'm doing some db operations. Can I use multi-threading concept to call async function "addInputs" so that it would execute faster?

`for(const temp of tx.vin) {
if (temp.txid) {
  let results =  await addInputs(temp.txid,temp.vout)
     inputs.push({
         "value": results[0],
         "address": results[1]
     });
   }
}`

Upvotes: 2

Views: 119

Answers (1)

Ferrybig
Ferrybig

Reputation: 18834

While JavaScript does not have multithreading, a trick you can use in this situation is to fill an array with promises, and then await them all at once:

const array = []
for(const id of ids) {
    array.push(addInputs (id));
}
const result =  await Promise.all(array);

Upvotes: 3

Related Questions