我第一次使用 Promise。我想从两个不同的端点获取数据(数组)。下面是我到目前为止想出的代码。我的控制台输出显示 [Promise, Promise]
两者均处于已解决状态。
我还可以看到我需要的响应data
,但我不知道如何访问它们。如何从 promise 中获取数据数组?谢谢
控制台中的 Promise 输出
0: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: "Object"
data: {room 18: {...}, room 19: {...}, room20: {...}} // this is what I need
1: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: "Object"
data: {room 18: {...}, room 19: {...}, room20: {...}} // this is what I need
const urls = ['/room/*/userconfig', '/room/*/observation/latest'];
let requests = urls.map(url => axios.get(url));
Promise.all([requests])
.then(res => res.map(response => console.log(response) // create 1 array and match roomId data))
最佳答案
当传递给 Promise.all
时,requests
数组不应位于另一个数组内
let requests = urls.map(url => axios.get(url));
Promise.all(requests)
.then(res => res.map(response => console.log(response));
关于javascript - 如何在 JavaScript 中访问已解析 Promise 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59032003/