【问题标题】:JavaScript: Promise chaining in foreach loopJavaScript:foreach 循环中的 Promise 链接
【发布时间】:2016-06-10 11:09:39
【问题描述】:

我是 javascript 承诺的新手,并且很难将它们与元素集合一起使用。在集合中,我执行了一个返回承诺的操作。完成整个操作(包括集合中的所有 Promise)后,我需要执行另一组操作。集合中的 Promise 需要按顺序排列。

我尝试了以下方法:

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

删除集合中的所有 conversations 后,我应该从 removeConference 返回什么?

【问题讨论】:

  • forEAch 循环在哪里还是我瞎了
  • Datsik:将编辑我的问题。
  • 嗨 Datsik,我已经更新了这个问题。我希望现在很清楚。

标签: javascript loops typescript promise


【解决方案1】:

所以这是我很早就理解承诺的东西。您需要让所有代码远离传递回调的做法,然后简单地使用 promise 来调用它。相反,为了保持 Promise 的连续性,您只想返回 Promise 给调用函数,除非您的函数应该决定之后要做什么。所以,你的代码应该是这样的。

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

我不能 100% 确定这是否正确编译,但希望它在概念上有所帮助。 Promise.all 确实是这里的关键函数 - 它接受一组承诺,并创建一个匹配的“控制承诺”,只有在所有承诺都拥有时才会解决。

【讨论】:

    猜你喜欢
    • 2018-05-20
    • 1970-01-01
    • 2014-07-09
    • 2018-05-26
    • 1970-01-01
    • 2023-04-06
    • 2016-10-21
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多