【发布时间】:2017-09-05 13:40:48
【问题描述】:
我想使用 REST API 从不同的服务器获取数据。
我这样做:
return fetch('http://localhost:9000/processes')
.then((response) => response.json())
.then((responseJson) => {
return responseJson;
})
.catch((error) => {
console.error(error);
});
在渲染函数中我有这个:
{JSON.stringify(getProcesses().Prozess1) }
还有这个
{JSON.stringify(getProcesses()) }
这两个例子中没有一个返回什么
其余的 API 应该返回这个:
{
"Prozess1": "Hallo ich bin ein Prozess"
}
在这种情况下我的问题是什么
【问题讨论】:
-
getProcesses()是返回承诺的异步操作吗? -
Yes getProcesses ist the Funktion which shoukd return the json
-
fetch() 是异步的,所以它不应该返回任何东西。 fetch().then() 是一个承诺,这意味着每当 fetch 完成时,请执行以下操作。所以稍微重构一下你的代码
标签: rest reactjs react-native fetch