我正在使用twilio平台...并且正在测试我的代码...但是当我尝试从channelDescriptor获取频道时我不明白会发生什么... 我有以下代码:
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返回一个Promise,然后我知道我需要像这样使用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 ..... step2 我在承诺链之外看到了。 我需要看这个结果:
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
第一个channelDescriptor ....所有的诺言都已执行....下一个ChannelDescriptor ...所有的诺言都已执行...换句话说,循环按每个channelDescriptor的顺序前进... 请我只需要Promises ...而不是Async / Await ...,因为我也需要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 ..... Step2 .....不在我的循环范围内。...我真的不明白。
答案 0 :(得分:0)
避免使用getPreviousMessages
中的Promise
constructor antipattern,请确保getChannel
始终返回承诺,并且不要忘记return
来then
processChannelPage
中的回调。
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);
// ^^^^^^^^^^^^^^^^ ^
};