React Native Fetch:第二个承诺挂起 [英] React Native Fetch: second promise hanging
问题描述
React Native的获取存在奇怪的问题. 以前它可以正常工作,不确定我所做的更改,但已停止工作.
Strange problem with React Native's fetch. It was working previously, not sure what I've changed but it's stopped working.
login(data,success,fail){
console.log('doing fb login');
fetch(host+'/api/login?credentials='+data.credentials)
.then( (response) => {
console.log('got login response');
return response.json();
} )
.then( json => {
console.log('got login json');
if(json.result!='fail'){
success(json);
} else {
fail(json);
}
return json;
})
.catch((error) => {
console.warn(error);
});
}
问题是我看到第一条获取登录响应"消息,但随后挂起,直到我按下它触发获取登录json"并按预期继续的屏幕,什么都没有发生.
The issue is I see the first 'got login response' message, but then it just hangs, and nothing happens until I press the screen upon which it fires the 'got login json' and continues as expected.
令人沮丧的是,这种情况一直持续发生,我不明白为什么第二个.then()不能自动触发.
It's frustrating because this is happening consistently and I can't see why the second .then() isn't firing automatically.
非常感谢您的帮助.
发现了类似的问题:可以做什么?会导致本机反应缓慢获取?
似乎已经在查看它: https://github.com/facebook /react-native/issues/6679
此外,只有在启用Chrome调试工具后才能看到该行为...有趣
Also the behaviour is only seen whne the Chrome debug tools are enabled... interesting
推荐答案
response.json() is a promise, not a value. So it won't resolve for you. I would also check result based on response.status instead of json.result because for some case server's response won't convertible to json (401 for example).
.then( (response) => {
if (response.status >= 200 && response.status < 300) {
response.json().then((data) => success(data));
} else {
fail(response);
}
})
这篇关于React Native Fetch:第二个承诺挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!