[英]Correct way to write arrow function in ES6
我正在尝试在ES6 JavaScript中执行以下操作,但是找不到正确的语法。 第二个.then()可以正常工作,但是第一个应该引发错误的.then()包含标准的JS语法)
return fetch('/api/data.json', {
credentials: 'same-origin'
}).then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
})
.then(response => response.json())
................
没有“魔术” :)关于它
.then(response => {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
})
您可以首先这样做.then()
:
.then(response => !response.ok ? throw Error() : response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.