当主体是单行时,箭头函数如何表现?
How does arrow functions behave when the body is a single line?
我想知道当箭头函数只有一行时,花括号和 return 关键字可以省略,它隐含地 returns 那里的任何东西。但是,如果我不想 return 任何东西怎么办。
例如,在下面的代码片段中,first .then
应该 return response.json
并且这就是代码所做的,该行被隐式 returned。但是在 second .then
中,我不想 return console.log
,但根据我的说法,它也被隐式地 returned.
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
我的问题是我是否理解正确?这真的很重要吗,因为它不会改变我的程序的结果?如果它能解决问题,那有什么办法呢?
But what if I don't want to return anything.
你最好的选择是使用没有 return
的冗长形式(带花括号)。但请注意,即便如此,调用函数 always 也会得到一个结果(即使函数没有 return
任何东西)。如果函数没有 return 任何东西,那么结果是 undefined
。
您 可以 (ab) 使用逗号运算符,但我不鼓励您这样做,您需要 ()
才能这样做:
.then(json => (console.log(json), void 0));
..尤其是因为它比
更冗长
.then(json => { console.log(json) });
(当然,无论如何,console.log
returns undefined
,所以...:-))
My question is have I understood this properly?
是的,你似乎(除了 次要 对“线”这个词的狡辩,请参阅下面的“旁注”)。
And does this really matter because it won't change the results of my program? And if it does what's the way around?
对于像 console.log
这样的函数,return 和 undefined.
当然没有关系,如果没有使用由最终 [=21] 编辑的承诺 return =],那就不,没关系。
FWIW,问题中代码的真实问题是:
它在调用 json
之前不寻找 HTTP 成功。这是 fetch
API 中的一个 footgun:它只拒绝 network 错误的承诺,而不是 HTTP 错误。
它不处理拒绝。
修复那些:
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.json();
.then(json => console.log(json))
.catch(error => {
// Handle/report error
});
旁注:
...when the body is a single line?
线条与此处无关。相关的是 =>
之后的第一个 non-whitespace 字符是或不是 {
。如果是,箭头函数有一个 函数体 并且如果你想 return 除了 undefined
之外的东西,则需要显式 return
。如果不是,箭头函数有一个简洁的主体,它是一个表达式,其结果是return从函数中编辑的。
have I understood this properly?
你理解的没错,你说的都很好
does this really matter because it won't change the results of my program?
这并不重要,它不会改变你程序的结果,如果你有第三个 then
它可能会改变它,因为 console.log
返回的值会变成第三个 then
回调的参数
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
.then(value => /*value is the value returned by console.log which is undefined*/)
但您不会被迫使用此值,这取决于您
what's the way around?
如果不想获取操作返回的值可以加花括号
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => {console.log(json)})
.then(value => /*still undefined because a function without return still return undefined by design of the language*/)
我想知道当箭头函数只有一行时,花括号和 return 关键字可以省略,它隐含地 returns 那里的任何东西。但是,如果我不想 return 任何东西怎么办。
例如,在下面的代码片段中,first .then
应该 return response.json
并且这就是代码所做的,该行被隐式 returned。但是在 second .then
中,我不想 return console.log
,但根据我的说法,它也被隐式地 returned.
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
我的问题是我是否理解正确?这真的很重要吗,因为它不会改变我的程序的结果?如果它能解决问题,那有什么办法呢?
But what if I don't want to return anything.
你最好的选择是使用没有 return
的冗长形式(带花括号)。但请注意,即便如此,调用函数 always 也会得到一个结果(即使函数没有 return
任何东西)。如果函数没有 return 任何东西,那么结果是 undefined
。
您 可以 (ab) 使用逗号运算符,但我不鼓励您这样做,您需要 ()
才能这样做:
.then(json => (console.log(json), void 0));
..尤其是因为它比
更冗长.then(json => { console.log(json) });
(当然,无论如何,console.log
returns undefined
,所以...:-))
My question is have I understood this properly?
是的,你似乎(除了 次要 对“线”这个词的狡辩,请参阅下面的“旁注”)。
And does this really matter because it won't change the results of my program? And if it does what's the way around?
对于像 console.log
这样的函数,return 和 undefined.
当然没有关系,如果没有使用由最终 [=21] 编辑的承诺 return =],那就不,没关系。
FWIW,问题中代码的真实问题是:
它在调用
json
之前不寻找 HTTP 成功。这是fetch
API 中的一个 footgun:它只拒绝 network 错误的承诺,而不是 HTTP 错误。它不处理拒绝。
修复那些:
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.json();
.then(json => console.log(json))
.catch(error => {
// Handle/report error
});
旁注:
...when the body is a single line?
线条与此处无关。相关的是 =>
之后的第一个 non-whitespace 字符是或不是 {
。如果是,箭头函数有一个 函数体 并且如果你想 return 除了 undefined
之外的东西,则需要显式 return
。如果不是,箭头函数有一个简洁的主体,它是一个表达式,其结果是return从函数中编辑的。
have I understood this properly?
你理解的没错,你说的都很好
does this really matter because it won't change the results of my program?
这并不重要,它不会改变你程序的结果,如果你有第三个 then
它可能会改变它,因为 console.log
返回的值会变成第三个 then
回调的参数
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
.then(value => /*value is the value returned by console.log which is undefined*/)
但您不会被迫使用此值,这取决于您
what's the way around?
如果不想获取操作返回的值可以加花括号
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => {console.log(json)})
.then(value => /*still undefined because a function without return still return undefined by design of the language*/)