【发布时间】:2018-10-02 14:35:47
【问题描述】:
这次我像这样创建了一个 Promise.all 调用。 tools.isMember 和 tools.unsubscribe 将返回 promise 对象。
tools.isMember(userid)
.then(results => {
Promise.all(
Object.keys(results).map((key, index) => {
tools.unsubscribe(results[index], userid)
})
)
})
.then(unsubscribe => { console.log('Unsubscribed Results => ', unsubscribed)})
.catch(err => console.log(err))
控制台打印
取消订阅的结果 => 未定义
我尝试将日志向上移动一点以进行调试,并将 console.log 放在 tools.unsubscribe 所在的位置
tools.isMember(userid)
.then(results => {
Promise.all(
Object.keys(results).map((key, index) => {
tools.unsubscribe(results[index], userid).then(result => { console.log('Result from Tools => " result) }) //Added Logging Here
})
)
})
.then(unsubscribe => { console.log('Unsubscribed Results => ', unsubscribe)})
.catch(err => console.log(err))
现在控制台显示
取消订阅的结果 => 未定义
工具的结果 => 1
所以现在我知道承诺正在从 tools.unsubscribed 返回预期结果,但是 Promise.all 应该返回包含所有结果的数组?现在显示未定义。
我尝试了许多不同的故障排除方法,但我是 Promise 的新手。一直试图弄清楚 Promise 出了什么问题。
更新了@Bergie:增加了 tools.unsubscribe 的回报
tools.isMember(userid)
.then(results => {
Promise.all(
Object.keys(results).map((key, index) => {
tools.unsubscribe(results[index], userid).then(result => { return result })
})
).then(result => { return result }) //Tryning to interpret Bergie's answer
})
.then(unsubscribe => { console.log('Unsubscribed Results => ', unsubscribed)})
.catch(err => console.log(err))
控制台打印
取消订阅的结果 => 未定义
【问题讨论】:
-
您没有从
then回调返回Promise.all()承诺,因此它使用undefined解析。 -
另外你没有从
map回调返回unsubscribe()承诺,所以Promise.all只会看到undefineds的数组。 -
@Bergi 你能看看我更新的代码吗?那是你的意思对吗?但问题是它仍然显示未定义。
-
不,
.then(result => { return result })is pointless。我是说results => { Promise.all(…) }和(key, index) => {…}回调中缺少return语句。
标签: javascript promise