javascript - 确定 Promises.all 中哪个 Promise 最慢

标签 javascript es6-promise

我一直在我的应用程序中使用 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/

相关文章:

javascript - 在 webdriver.io 中的 while 循环中链接 Promise

javascript - 我怎样才能摆脱这个 Promise for 循环?

javascript - 获取 API 全局错误处理程序

javascript - 如何将 javascript 和 css 文件添加到 magentomodule

javascript - 将 promise 返回的值推送到数组中

javascript - 使用 ng-csv 创建 tsv 文件

javascript - 从 JavaScript 中的循环创建的对象,如何在 json 中分析它们

javascript - 将 Promise 与事件相结合

javascript - return 会停止循环吗?

javascript - 异步/等待 -> 可能没有返回值吗?