【问题标题】:Promise.All with Object Mapping - Undefined带有对象映射的 Promise.All - 未定义
【发布时间】:2018-10-02 14:35:47
【问题描述】:

这次我像这样创建了一个 Promise.all 调用。 tools.isMembertools.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


【解决方案1】:

你需要从你的地图返回一个承诺,目前你没有返回任何东西,这相当于未定义,在主体块周围带有花括号的箭头函数需要显式返回,所以你要么添加它,要么删除花括号.

你也不需要地图中的then,你需要返回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)}) //unsubscribe is an array
            .catch(err => console.log(err))
        })

编辑:

then() 是 Promise 对象的一个​​方法,所以你应该在 Promise 的实例上调用它。详情请参阅the MDN documentation page

我将 return 放入箭头函数体或删除花括号 { 和 } 的意思是

(x, y) => x + y 
//is equivalent to 
(x, y) => {
   return x + y
}
//while
(x, y) => {
    x + y
}
//is equivalent to
(x, y) => {
    x + y
    return
}

【讨论】:

  • 哇,好用!我猜我放 .then 的地方是错误的。有时将下一个放在哪里令人困惑。你能告诉我你返回的花括号是什么意思吗?
猜你喜欢
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多