【发布时间】: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