我如何知道我的promise.all是否并行运行? [英] How can I tell if my promise.all is running in parallel?
本文介绍了我如何知道我的promise.all是否并行运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下Promise.all
示例.我想知道它是否在lambda.invoke
方面并行"运行?以及我该如何测试某些东西是否正在并行运行?
I have the following Promise.all
example. I was wondering if it was operating in "parallel" in regards to the lambda.invoke
? And how can I test if something is running parallel?
引用了此线程
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);
});
推荐答案
您可以算出已经开始但尚未完成的操作数:
You could count how many actions are started and haven't finished yet:
let running = 0;
// Inside of the promise:
running++;
lambda.invoke(params, (err, data) => {
running--;
console.log(`lambda.invoked finished, ${running} tasks still running`);
});
如果invoke
是同步的,则会得到:
If invoke
is synchronous, you get:
lambda.invoked finished, 0 tasks still running
lambda.invoked finished, 0 tasks still running
lambda.invoked finished, 0 tasks still running
如果它是异步的,则会得到:
If it is asynchronous you get:
lambda.invoked finished, 2 tasks still running
lambda.invoked finished, 1 tasks still running
lambda.invoked finished, 0 tasks still running
这篇关于我如何知道我的promise.all是否并行运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文