本文介绍了确定Promises.all中最慢的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在我的应用程序中使用Promise.all。
为了提高应用程序速度,如何确定哪个承诺最慢?
I have been using Promise.all in my app. For the purpose of improving app speed, how to determine which promise is the slowest?
const result = await Promise.all([
this.props.fetchUser(),
this.props.cacheResourcesAsync(),
this.props.initAmplitude(),
this.props.initAppVariables(),
]);
推荐答案
您可以为此使用辅助功能:
You can use a helper function for that:
async function time(p, name) {
const start = Date.now();
try {
return await p;
} finally {
const end = Date.now();
console.log(`${name} took ${end-start}ms`);
}
}
然后写
const result = await Promise.all([
time(this.props.fetchUser(), "user"),
time(this.props.cacheResourcesAsync(), "cacheResources"),
time(this.props.initAmplitude(), "amplitude"),
time(this.props.initAppVariables(), "appVars"),
]);
这篇关于确定Promises.all中最慢的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!