简体   繁体   English

为什么我们需要返回 promise 解析?

[英]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.下面是ab的区别。 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.

相关问题 为什么我们需要JS中的Promise - Why do we need Promise in JS 在异步函数中,我们是返回 X 还是返回“Promise.Resolve(X)”? - In async functions, do we return X, or do we return "Promise.Resolve(X)"? 我们是否需要使用async / await将返回值包装到promise中? - Do we need to wrap return value into promise using async/await? 使用 Promise,为什么浏览器会返回两次拒绝但两次没有解析? - With a Promise, why do browsers return a reject twice but not a resolve twice? 为什么我们需要将 Observable 转换为 Promise 以获得已解析的值? - Why do we need to convert an Observable to Promise to get a resolved value? 为什么在返回 Promise 时需要显式返回? - Why do I need an explicit return when a Promise is returned? 为什么我们在承诺链中使用 return ,即使我们已经在函数中返回了? - Why do we use return in promise chain even we already have return in the function? 为什么我需要在承诺中解决响应 - Why i need to resolve a response in promise 我们如何从 Redux - saga 中的 store.dispatch 返回 Promise - saga,以便我们可以等待解析然后在 SSR 中渲染? - How do we return a Promise from a store.dispatch in Redux - saga so that we can wait for the resolve and then render in SSR? 如果我们通常需要在继续之前处理一个承诺,为什么不返回一个已经处理的响应而不是一个承诺的响应? - If we typically need a promise to be handled before moving on, why not return an already-handled response instead of a promised response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM