Node.js 我们可以使用多线程并行运行async()吗?
在循环中,我正在执行一些db操作。我可以使用多线程的概念来调用异步函数“addInputs”以便它执行得更快吗Node.js 我们可以使用多线程并行运行async()吗?,node.js,multithreading,Node.js,Multithreading,在循环中,我正在执行一些db操作。我可以使用多线程的概念来调用异步函数“addInputs”以便它执行得更快吗 `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] }); }
`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]
});
}
}`
虽然JavaScript没有多线程,但在这种情况下,您可以使用一种技巧,用承诺填充数组,然后立即等待它们:
const array = []
for(const id of ids) {
array.push(addInputs (id));
}
const result = await Promise.all(array);
是的,你能做到。