如何在返回的承诺中引用数据

问题描述 投票:0回答:1

如何访问Promise.Object.collection中的数据数组?enter image description here

javascript fetch es6-promise
1个回答
0
投票

基本上你已经拥有了一切。你需要做的就是在地图上设置每个if和itacate的返回值,地图上的每一个项目都是集合的对象。

function fetchRequests (){
....
}).then(responde => response.json())
.then(responde => {
    const { result, collection } = response;
   If (result===“success”) {
      return collection.map(item => new Person(item))
   }
})

你需要在集合上为你得到的对象创建一个新的类。

class Perdon {

    constructor (data) {
        Object.assign(this, data);
   }
}

在你调用方法fetchRequests的任何地方。

async function funcCaller () {
   const people = await fetchRequests();
   // people have your collection 
}
© www.soinside.com 2019 - 2024. All rights reserved.