Greg Gum Greg Gum - 1 month ago 12
TypeScript Question

How to use Promise.All with Typescript

Here is what I want to do:

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


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

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

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();

Answer

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();
});