【发布时间】:2016-09-25 22:58:48
【问题描述】:
有没有人举例说明如何将 Promise 与 GraphRequestManager 一起使用? 我在我的动作创建器中收到无法读取属性然后出现未定义错误。
function graphRequest(path, params, token=undefined, version=undefined, method='GET') {
return new Promise((resolve, reject) => {
new GraphRequestManager().addRequest(new GraphRequest(
path,
{
httpMethod: method,
version: version,
accessToken: token
},
(error, result) => {
if (error) {
console.log('Error fetching data: ' + error);
reject('error making request. ' + error);
} else {
console.log('Success fetching data: ');
console.log(result);
resolve(result);
}
},
)).start();
});
}
我使用我的动作创建器调用上述内容
export function accounts() {
return dispatch => {
console.log("fetching accounts!!!!!!");
dispatch(accountsFetch());
fbAPI.accounts().then((accounts) => {
dispatch(accountsFetchSuccess(accounts));
}).catch((error) => {
dispatch(accountsFetchFailure(error));
})
}
}
我在控制台中收到“成功获取数据:”以及错误前的结果。至此 API 调用成功。错误是在获取 fbAPI.accounts().then((accounts) 中的帐户之后,我认为这是由于 GraphRequestManager 立即返回而不是等待。
【问题讨论】:
标签: react-native react-redux es6-promise react-native-fbsdk