JavaScript:foreach循环中的承诺链

JavaScript:foreach循环中的承诺链,javascript,loops,typescript,promise,Javascript,Loops,Typescript,Promise,我不熟悉javascript承诺,在将其用于元素集合时遇到困难。在集合中,我执行一个返回承诺的操作。一旦整个操作(包括集合中的所有承诺)完成,我需要执行另一组操作。集合中的承诺需要按顺序进行 我尝试过以下方法: public cleanup(onCleanupComplete: any): void { if (this._app == null) return; //this._app comes out of an external API // Below

我不熟悉javascript承诺,在将其用于元素集合时遇到困难。在集合中,我执行一个返回承诺的操作。一旦整个操作(包括集合中的所有承诺)完成,我需要执行另一组操作。集合中的承诺需要按顺序进行

我尝试过以下方法:

public cleanup(onCleanupComplete: any): void {
        if (this._app == null) return; //this._app comes out of an external API

       // Below line of code won't compile, it is just for illustration. 
       // I'm trying to show that I need a promise in return from method
        this.removeConference(0).then(() => {
              // Do additional clean up operation and call onCleanupComplete
                onCleanupComplete(true, null);                
        });

    }

    private removeConference(i : number) {
        if (this._app.conversationsManager.conversations == null 
           || i === this.conversationLength)
            return; // this.conversationLength equals initial 
                    // number of elements in collection 
                    // How do I return a promise here?

        var conversation = this._app.conversationsManager.conversations(0);
                console.log("app.cleanup.leave", `Leaving conversation ${conversation}`);
        conversation.leave().then(() => {
                console.log("app.cleanup.leave", `Conversation ${conversation} left successfully`);
            this.app.conversationsManager.conversations.remove(conversation);
 _           this.removeConference(i);
        });
    }

一旦删除了集合中的所有
对话,我应该从removeConference返回什么?

因此,这是我在理解承诺的早期就抓住了的东西。您需要让所有代码远离传入回调的做法,然后简单地使用承诺调用回调。相反,为了保持承诺的连续性,您只需要将承诺返回给调用函数,除非您的函数应该决定以后要做什么。因此,您的代码应该是这样的

public cleanup(onCleanupComplete: any):Promise<any> {
        if (this._app == null) return; //this._app comes out of an external API

       // Below line of code won't compile, it is just for illustration. 
       // I'm trying to show that I need a promise in return from method
       var promiseArray = [];
       for (var i = 0; i < this.conversationLength; i++) {
         promiseArray.push(removeConference(i));
       }
       return Promise.all(promiseArray);

    }

    private removeConference(i : number):Promise<any> {
        if (this._app.conversationsManager.conversations == null 
           || i === this.conversationLength)
            return Promise.resolve(true);

        var conversation = this._app.conversationsManager.conversations(0);
                console.log("app.cleanup.leave", `Leaving conversation ${conversation}`);
        return conversation.leave().then(() => {
                console.log("app.cleanup.leave", `Conversation ${conversation} left successfully`);
            this.app.conversationsManager.conversations.remove(conversation);
            this.removeConference(i);
        });
    }
公共清理(onCleanupComplete:any):承诺{
if(this.\u app==null)返回;//this.\u app来自外部API
//下面的代码行不会编译,它只是为了说明。
//我试图表明,我需要一个承诺作为方法的回报
var promiseArray=[];
for(var i=0;i{
log(“app.cleanup.leave”,`Conversation${Conversation}left成功`);
this.app.conversationsManager.conversations.remove(会话);
本.删除会议(i);
});
}

我不能百分之百确定这是否正确编译,但希望它在概念上有所帮助<代码>承诺。所有
实际上是这里的关键功能-它接受一系列承诺,并创建一个匹配的“控制承诺”,只有当所有承诺都有时才会解决。

forEAch循环在哪里或我是盲的Datsik:将编辑我的问题。嗨,Datsik,我已经更新了问题。我希望现在一切都清楚了。