React native 何时返回fetch()以及何时仅使用fetch()请求?
我从react native开始,我一直想知道的是,有时我看到fetch是这样使用的:React native 何时返回fetch()以及何时仅使用fetch()请求?,react-native,fetch-api,React Native,Fetch Api,我从react native开始,我一直想知道的是,有时我看到fetch是这样使用的: createTodo(){ fetch('http://192.168.1.34:3000/createTodo', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body:
createTodo(){
fetch('http://192.168.1.34:3000/createTodo', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: this.state.noteText,
}),
}).then((response) => response.json())
.then((responseJson) => {
var d = new Date();
this.state.noteArray.push({
'date':d.getFullYear()+
"/"+(d.getMonth()+1) +
"/"+ d.getDate(),
'note': responseJson.data.content
});
this.setState({ noteArray: this.state.noteArray });
this.setState({noteText:''});
console.log(responseJson);
}).catch((error) => {
console.error(error);
console.log('Shit! Error occured');
});
}
这工作做得很好
有时是:
return fetch(...)...
我有点困惑。取回是承诺,它会返回另一个承诺。已解析的结果传递给下一个。然后输入参数。因此,在示例代码中,您可以处理fetch函数传递的响应值
当createTodo的客户端希望使用createTodo的“结果”时,可以返回fetch函数。“result”是另一个Promise,其输入参数来自createTodo的返回值
演示链接:
演示只是为了表明承诺的回报值是另一个承诺。我希望你能得到暗示