【发布时间】:2019-01-25 00:15:38
【问题描述】:
我收到了一个从 POST 调用到 express 后端的承诺响应。我想在 Promise 中插入一个 if 语句,这似乎违反了它。
这行得通:
fetch('http://localhost:8000/upload', {
method: 'POST',
body: formData,
})
.then(response =>
response.json())
.then(response => {
console.log(response);
})
这个 if else 语句破坏了代码:
fetch('http://localhost:8000/upload', {
method: 'POST',
body: formData,
})
.then(response => {
if (response.ok) {
response.json()
} else {
throw new Error('Something went wrong ...');
}
})
.then(response => {
console.log(response);
})
这样的事情可能吗?提前致谢!
【问题讨论】:
标签: javascript reactjs express post promise