我一直在我的应用程序中使用 Promise.all。 为了提高app速度,如何确定哪个promise最慢?
const result = await Promise.all([
this.props.fetchUser(),
this.props.cacheResourcesAsync(),
this.props.initAmplitude(),
this.props.initAppVariables(),
]);
最佳答案
您可以使用辅助函数来实现:
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"),
]);
关于javascript - 确定 Promises.all 中哪个 Promise 最慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861636/