Javascript Chain of promises in For Loop



我正在使用twilio平台...我正在测试我的代码...但我不明白当我尝试从频道描述符获取频道时会发生什么...... 我有这个代码:

function processChannelPage(page)
{
var items = page.items;
that.channels = that.channels || [];
for (let c = 0, p = Promise.resolve(); c < items.length; c++) 
{
p = p.then(function() {
let channelDescriptor = items[c];                        
console.log("SID ", channelDescriptor.sid);


getPreviousMessages(channelDescriptor, that)
.then(function() {
console.log("resuelto msg", channelDescriptor.sid);
return Promise.resolve();
}); 
});    
}
..............
}
that.client.getUserChannelDescriptors().then(function (paginator) {
processChannelPage(paginator);
});

function getPreviousMessages(channelDescriptor, chat) {
return new Promise(resolve => { 
console.log("p2.....step0.....", channelDescriptor.sid);     
console.log("p2.....step1.....", channelDescriptor.sid);
let promise = chat.getChannel(channelDescriptor.sid); 
console.log(promise);       
return promise.then(channel => {
console.log("p2.....step2.....", channelDescriptor.sid); 
return channel;
});
});
}

TwilioChat.prototype.getChannel = function (channel_sid) {
console.log("I am in getChannel.....");
for (var channel in this.channels) {
if (this.channels[channel].sid == channel_sid) {
return this.channels[channel].getChannel();
}
}
return null;
};

我知道TwilioChat.prototype.getChannel返回了一个承诺,那么我知道我需要像这样使用THEN来评估这个承诺

chat.getChannel(channelDescriptor.sid).then

但我看到这个结果:

SID  CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:147 p2.....step0..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:148 p2.....step1..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:72 SID  CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:147 p2.....step0..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:148 p2.....step1..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:150 p2.....step2..... CH369ca3cc46d74886b031a52fd2e7dc29

我的问题是...为什么这个日志twilio_helper.js:150 p2.....步骤2我看到我的应许链之外。 我需要看到这个结果:

SID  CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:147 p2.....step0..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:148 p2.....step1..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:72 SID  CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:147 p2.....step0..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:148 p2.....step1..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH369ca3cc46d74886b031a52fd2e7dc29

第一个通道描述符....所有承诺都已执行。下一个频道描述符...所有承诺都已执行...换句话说,循环按每个通道描述符的顺序前进... 请我只需要承诺...不是异步/等待...因为我需要这也适用于IExplorer。 你能帮我兑现这个承诺吗? 谢谢!

还行!!!! 好的,我像这些修改一样更改我的代码....

TwilioChat.prototype.getChannel = function(channel_sid){
console.log("I am in getChannel.....");
for (var channel in this.channels) {
if (this.channels[channel].sid == channel_sid) {
return this.channels[channel].getChannel();
}
}
reject("error");
};
function getPreviousMessages(channelDescriptor, chat) {
return new Promise(resolve => { 
console.log("p2.....step0.....", channelDescriptor.sid);     
console.log("p2.....step1.....", channelDescriptor.sid);
let promise = chat.getChannel(channelDescriptor.sid); 
console.log(promise);       
return promise.then(channel => {
console.log("p2.....step2.....", channelDescriptor.sid); 
resolve(channel); // I Resolve my New Promise
});
});
}

但我的测试总是得到这个结果:

明白了。。。。此代码日志....

SID  CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:147 p2.....step0..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:148 p2.....step1..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:72 SID  CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:147 p2.....step0..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:148 p2.....step1..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:150 p2.....step2..... CH369ca3cc46d74886b031a52fd2e7dc29

p2.....步骤2.....在我的循环之外....我真的不明白。

避免在getPreviousMessages中使用Promise构造函数反模式,确保getChannel始终返回一个 promise,并且不要忘记从processChannelPage中的then回调中return您的承诺。

function processChannelPage(page) {
var items = page.items;
that.channels = that.channels || [];
for (let c = 0, p = Promise.resolve(); c < items.length; c++) {
p = p.then(function() {
let channelDescriptor = items[c];                        
console.log("SID ", channelDescriptor.sid);
return getPreviousMessages(channelDescriptor, that).then(function() {
//          ^^^^^^
console.log("resuelto msg", channelDescriptor.sid);
}); 
});
}
…
return p;
}

that.client.getUserChannelDescriptors().then(function (paginator) {
return processChannelPage(paginator);
//  ^^^^^^
});

function getPreviousMessages(channelDescriptor, chat) {
console.log("p2.....step0.....", channelDescriptor.sid);     
console.log("p2.....step1.....", channelDescriptor.sid);
let promise = chat.getChannel(channelDescriptor.sid); 
console.log(promise);       
return promise.then(channel => {
//  ^^^^^^
console.log("p2.....step2.....", channelDescriptor.sid); 
return channel;
});
}

TwilioChat.prototype.getChannel = function (channel_sid) {
console.log("I am in getChannel.....");
for (var channel in this.channels) {
if (this.channels[channel].sid == channel_sid) {
return this.channels[channel].getChannel();
}
}
return Promise.resolve(null);
//         ^^^^^^^^^^^^^^^^    ^
};

最新更新