如何在 Typescript 中使用 Promise.all() [英] How to use Promise.all() with Typescript

查看:123
本文介绍了如何在 Typescript 中使用 Promise.all()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要做的:

Promise.all([aurelia.start(), entityManagerProvider.initialize()])
    .then((results:Array<any>) => {
        let aurelia: any = results[0];
        aurelia.setRoot();
    });

aurelia.start() 返回 Aurelia 类型,而 initialize() 返回 void.

aurelia.start() returns an Aurelia type, while initialize() returns void.

编译器给出一个错误信息,不能从用法推断出类型.

The compiler gives an error message that the type cannot be inferred from the usage.

我想要实现的是让它们同时运行,因为它们都是很长的进程,然后运行 ​​Aurelia.setRoot();

What I am trying to achieve is to get them to run at the same time, as they are both very long processes, then run Aurelia.setRoot();

推荐答案

这是 TypeScript 及其 Promise.all 签名的弱点.通常最好拥有类型一致的数组.不过,您可以手动执行以下操作:

This is a weakness in the TypeScript and its Promise.all signature. Its generally best to have arrays with consistent types. You can do the following manually though:

let foo : [Promise<Aurelia>,Promise<void>] = [aurelia.start(), entityManagerProvider.initialize()];
Promise.all(foo).then((results:any[]) => {
    let aurelia: any = results[0];
    aurelia.setRoot();
});

这篇关于如何在 Typescript 中使用 Promise.all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆