react-native fetch async / await response filtering

时间:2017-10-24 20:01:37

标签: javascript react-native promise async-await fetch-api

我的服务器响应如buildings : [record1, record2, ...],我想从该响应中获取数组。我怎样才能从Promise获得数组?我尝试了一些异步/等待的东西,但我不明白如何在这段代码中使用它:

setupImpagination() {
    ....
    fetch(pageOffset, pageSize, stats) {
        return fetch(`http://localhost:3000/api/building/all?skip=${pageOffset}&limit=${pageSize}`)
            .then(response => {
            console.log('response.json() => ',response.json());
            response.json()
            })
            .catch((error) => {
            console.error(error);
            });

    }
    });
}

2 个答案:

答案 0 :(得分:14)

。然后

 fetch(pageOffset, pageSize, stats) {
        return fetch(`http://localhost:3000/api/building/all?skip=${pageOffset}&limit=${pageSize}`)
            .then(response => {
               console.log('response.json() => ',response.json());
               response.json()
            }).then(responseJson => {
               return responseJson.buildings
            }).catch((error) => {
               console.error(error);
            });

        }
    });

async / await方式:

 fetch(pageOffset, pageSize, stats) {
       try {
           const response = await fetch(`http://localhost:3000/api/building/all?skip=${pageOffset}&limit=${pageSize}`);
           const responseJson = await response.json();
           return responseJson.buildings;
       } catch(error){
            console.error(error);
       }

答案 1 :(得分:0)

  bmidata <- data [,"bmi_0m"] 
    bmirange<-cut (bmidata, breaks=c(0,18.5,25,30,100), labels=c("<18.5", "18.5-24.99", "25-29.99", ">30"), include.lowest = TRUE)
    table (bmirange)
    prop.table(table (bmirange))

作为ES7上的原始js,不以本机为目标