【问题标题】:Javascript await object property to be setJavascript 等待对象属性被设置
【发布时间】:2018-06-05 19:30:03
【问题描述】:
getStore().then((store) => {
  buildRates().then((rates) => {
    let newStore = store;
    newStore.rates = rates;
    setStore(newStore).then((res) => {
      // callback
      cb(null, res);
    })
  })
});

用 async/await 语法写这个有问题。当我尝试写这样的等待时......

let store = await getStore();
store.rates = await buildRates();
setStore(store).then((res) => {
  // callback
  cb(null, res);
});

...setStore 使用从await getStore() 返回的原始对象,在store.rates 上没有来自await buildRates() 的对象

有什么想法吗?

【问题讨论】:

  • 无法复制:jsfiddle.net/kb51h92y
  • 我假设 buildRates 不再返回一个 Promise,或者这个 Promise 被解析为一个你不期望的值。

标签: javascript node.js asynchronous ecmascript-6 async-await


【解决方案1】:

解决了。问题是 buildRates() 不是一个正确的承诺,哎呀!

【讨论】:

  • 因此,问题出在您未显示的代码中。我建议你删除这个问题,因为它对其他任何人作为未来的参考毫无用处。
  • 无法删除 sry :S
猜你喜欢
  • 2013-04-11
  • 2015-03-14
  • 2021-05-24
  • 2021-03-18
  • 2018-12-02
  • 2019-07-20
  • 2018-12-09
  • 2019-03-24
  • 2014-11-18
相关资源
最近更新 更多