[英]Why do we need to return a promise resolve?
async function f() { let promise = new Promise((resolve, reject) => { setTimeout(() => resolve("done,"); 1000) }); let result = await promise; // wait until the promise resolves (*) return result. // "done." } f();then(result => { return Promise.resolve(result). }).then(r => console.log(r))
The above code does not work as intended if I remove the return keyword from the second last line.如果我从倒数第二行中删除return关键字,则上述代码无法按预期工作。 I do not understand why do we need to write a return ?
我不明白为什么我们需要写return ? Does not the resolve method of promise essentially does that ie return a value?
promise 的解析方法本质上不就是返回一个值吗?
I do not understand why do we need to write a return?
不明白为什么要写return?
Because if you don't, the fulfillment callback's return value will be undefined
, as with any other function, which means that the fulfillment value of the promise created by that then
call will be undefined
instead of being result
.因为如果你不这样做,实现回调的返回值将是
undefined
,与任何其他 function 一样,这意味着由then
调用创建的 promise 的实现值将是undefined
而不是result
。
There's no reason for that fulfillment handler at all, it doesn't do anything useful, it just introduces an extra async "tick" into the promise fulfillment.该履行处理程序根本没有理由,它没有做任何有用的事情,它只是在 promise 履行中引入了一个额外的异步“滴答”。 Just remove it:
只需将其删除:
f().then(result => console.log(result))
In a comment you've said:在评论中你说:
I understand that the part was not needed at all to be added.
我知道根本不需要添加该部分。 but I added it to understand Promises and in principle it should work, in my opinion.
但我添加它是为了理解 Promises,在我看来,原则上它应该可以工作。
It's for the reason I mentioned in the first paragraph above: otherwise, the function returns undefined
(implicitly).这是因为我在上面第一段中提到的原因:否则,function 返回
undefined
(隐式)。 It's the difference between a
and b
in the following.下面是
a
和b
的区别。 You might be thinking of concise arrow syntax where the body of the function is just an expression and the return
is implicit, like c
below:您可能正在考虑简洁的箭头语法,其中 function 的主体只是一个表达式,并且
return
是隐式的,例如下面的c
:
const a = () => { 42; }; const b = () => { return 42; }; const c = () => 42; // No `{` just after the `=>` console.log(a()); // undefined console.log(b()); // 42 console.log(c()); // 42
In that case, you'd use:在这种情况下,您将使用:
f().then(result => result) // If you want that there to understand it better
.then(result => console.log(result))
Note there's no Promise.resolve
there.请注意,那里没有
Promise.resolve
。 There's no reason to create another promise;没有理由再创建一个 promise; the return value of the fulfillment handler will be used to resolve the promise
then
returned.履行处理程序的返回值将用于解析 promise
then
返回。 No need for an extra one.不需要额外的。
First, you can remove the entire middle then
:首先,您可以删除整个中间
then
:
f().then(r => console.log(r))
Second, the last then
expects an input from the previous one: r
, which is why we should return
it.其次,最后一个
then
期望前一个的输入: r
,这就是我们应该return
它的原因。
and last,最后,
.then((result) => Promise.resolve(result))
is the same as:是相同的:
.then((result) => result)
Note the subtle difference between this:请注意这之间的细微差别:
f().then(result => {
Promise.resolve(result);
}).then(r => console.log(r)); // prints "undefined"
compared to this:与此相比:
f().then(result =>
Promise.resolve(result)
).then(r => console.log(r)); // prints resolved value of `f`
In the first example, you are supplying multiple statements (inside the brackets) to the then
handler.在第一个示例中,您向
then
处理程序提供了多个语句(在括号内)。 Because you are not explicitly returning a value, the return value is undefined, which is what will be seen by subsequent then
handler.因为你没有显式地返回一个值,所以返回值是未定义的,这将被后续的
then
处理程序看到。
In the second example, the arrow function is provided a simple expression (there are no brackets, and only a single line is supplied).在第二个示例中,箭头 function 提供了一个简单的表达式(没有括号,只提供了一行)。 In this case, it is the expression itself which provides the return value for the function.
在这种情况下,为 function 提供返回值的是表达式本身。
This documentation on arrow function syntax is a bit more precise.这个关于箭头 function 语法的文档更精确一些。
As others have pointed out, in this contrived example the Promise.resolve()
is not needed at all, as the value given to the then
handler is indeed already resolved.正如其他人指出的那样,在这个人为的例子中,
Promise.resolve()
根本不需要,因为给then
处理程序的值确实已经解决了。 So you could just as well do:所以你也可以这样做:
f().then(r => console.log(r));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.