如何向单个Promise All Array添加void Promise和其他类型的promise

时间:2016-08-28 15:23:29

标签: javascript typescript promise

Promise.all适用于所有promise<void>这样的

var actionList = new Array<Promise<void>>();

actionList.push(PluginService.InstallExtension(element, en.ExtensionFolder)
    .then(function () {
        addedExtensions.push(element);
        var name = element.publisher + '.' + element.name + '-' + element.version;
        //vscode.window.showInformationMessage("Extension " + name + " installed Successfully");
    }));

Promise.all(actionList).then(function () {
    // all resolved
}).catch(function (e) {
    console.error(e);
});

我想在Promise<boolean>

中添加actionList

我如何添加打字稿?

1 个答案:

答案 0 :(得分:2)

在type参数上使用union type并指定它可以是void或布尔值:

var actionList = new Array<Promise<void | boolean>>();

// example of compiling code:
actionList.push(new Promise<void>((resolve, reject) => {}));
actionList.push(new Promise<boolean>((resolve, reject) => {}));