我的响应JSON数据返回承诺

时间:2018-08-20 12:17:47

标签: javascript json reactjs react-native fetch-api

我正在使用javascript,当我请求数据返回承诺时,我尝试等待,然后却无法正常工作。提取api数据该怎么办?

_getMatch = () => {
     GetMatchesByAccountID(this.state.user.accountId,this.props.login.login_region)
    .then(res => {
        var i;
        let matchesList = res.matches;
        matchesList = matchesList.slice(0,5)
        for(i=0; i<matchesList.length; i++){

            matchesList[i].matchInfo = GetSingleMatchByMatchID(matchesList[i].gameId,this.props.login.login_region)
            console.log(matchesList[i].matchInfo)
        }
        console.log(matchesList)
        this.setState({match : matchesList})
    }).catch(e => {
        console.error(e)
    })
}

以及我在GetMatchesByAccountID内部使用的其他方法

export const GetSingleMatchByMatchID = (matchID,region) =>  new Promise((resolve,reject) => {
try{
    const headers = new Headers();
    headers.append("X-Riot-Token", "RGAPI-1009f01f-17db-4132-ab3c-******");
    fetch('https://' + region + '/lol/match/v3/matches/' + matchID, {
        headers: headers,
        method: 'get'
    })
    .then(res =>{
        res.json()
        .then(jres =>{
            resolve(jres);
        }).catch(e =>{
            reject(e);
        })
    }).catch(e => {
        reject(e);
    })
}
catch(e){
    reject(e);
}

})

0 个答案:

没有答案