gpt4 book ai didi

javascript - 我如何判断我的 promise.all 是否并行运行?

转载 作者:行者123 更新时间:2023-11-30 08:22:03 25 4
gpt4 key购买 nike

我有以下 Promise.all 示例。我想知道它在 lambda.invoke 方面是否“并行”运行?我如何测试某些东西是否并行运行?

引用此 thread

function get1(id) {
return new Promise((resolve, reject) => {
const params = {
FunctionName: 'myLambda', // the lambda function we are going to invoke
InvocationType: 'RequestResponse',
Payload: { id },
};

lambda.invoke(params, (err, data) => {
if (err) {
reject(new Error('error'));
} else {
const result = JSON.parse(data.Payload);
resolve(result);
}
});
}).catch(e => Promise.resolve({ error: 'Error has occurred.' }));
}

exports.getDetails = list => Promise.all(list.map(get1))
.then((response) => {
return result;
}).catch((error) => {
console.log('oops ', error);
});

最佳答案

你可以计算有多少 Action 已经开始但还没有完成:

  let running = 0;

// Inside of the promise:
running++;
lambda.invoke(params, (err, data) => {
running--;
console.log(`lambda.invoked finished, ${running} tasks still running`);
});

如果 invoke 是同步的,你会得到:

  lambda.invoked finished, 0 tasks still running
lambda.invoked finished, 0 tasks still running
lambda.invoked finished, 0 tasks still running

如果它是异步的,你会得到:

  lambda.invoked finished, 2 tasks still running
lambda.invoked finished, 1 tasks still running
lambda.invoked finished, 0 tasks still running

关于javascript - 我如何判断我的 promise.all 是否并行运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com