javascript - 访问链中先前 Promise 的数据

标签 javascript ecmascript-6 es6-promise

我的问题是我想访问从之前的 then() 获取的数据,我该怎么做? (要求:我无法修改 externalBuiltInFunction() )

ajaxCall(...)
.then( (response) => {                          
    return response.json();
})
.then ( (jsonData) => {
    return externalBuiltInFunction(jsonData);
})
.then ((dataFromExternalFunction) => {
   ... here i want to use jsonData, how can i do ?...
}

感谢帮助

最佳答案

您可以仅使用一个 then 语句与 async/await:

ajaxCall(...)
  .then(async response => {                          
    const jsonData = await response.json();
    const external = await externalBuiltInFunction(jsonData);
    // Here you still have access to jsonData and external 
  })

关于javascript - 访问链中先前 Promise 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56955696/

相关文章:

javascript - 使用 window.location.replace 有没有办法更改端口?

javascript - Rails 4/jQuery : Need entry of value in a form input to autofill other inputs

javascript - 如何防止 react 重新渲染整个组件?

javascript - Promise 后的火灾事件

javascript - 超出最大更新深度。当组件重复调用 setState 时可能会发生这种情况

javascript - 函数未在 Bootstrap 弹出窗口内容 div 中设置内部 HTML

javascript - 从 computeDigest(algorithm, value) byte[] 取回字符串表示

javascript - 这个变量在ES6中是全局的吗?

javascript - 如何使用Lodash根据对象的嵌套属性值选择对象的属性?

javascript - forEach 循环(异步)完成后返回数组