javascript 解析来自 ES6 箭头函数的 promise
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46629698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resolving promises from ES6 Arrow Functions
提问by const
Reading the docs as I understand it in ES6 the meaning of:
阅读我在 ES6 中理解的文档的含义:
foo => someFun(foo);
is equivalent to:
相当于:
foo => { return someFun(foo); }
I'm returning a new Promise and within that code using arrow functions to invoke the resolve & reject methods, e.g.
我正在返回一个新的 Promise 并在该代码中使用箭头函数调用解析和拒绝方法,例如
return new Promise(function(resolve, reject)
{
someFunThatReturnsAPromise()
.then(data => resolve(data))
.catch(err => reject(err));
});
As such is the code in the then actually,
因此实际上是当时的代码,
.then(data => return resolve(data))
If so, does it matter that the result of resolve (of which I'm not sure of the type of value) and should I instead be slightly less terse and write it using {} to prevent the implicit return
如果是这样,解析的结果(我不确定值的类型)是否重要,我是否应该稍微简洁一些并使用 {} 编写它以防止隐式返回
.then(data => { resolve(data); })
回答by Lennholm
The resolvefunction already returns undefined, so it makes absolutely no difference if you implicitly return it with a one-line arrow function or don't return it from a function body at all (since the latter means your function body implicitly returns undefineditself).
该resolve函数已经返回undefined,因此如果您使用单行箭头函数隐式返回它或根本不从函数体返回它(因为后者意味着您的函数体隐式返回undefined自身),这绝对没有区别。
Furthermore, since you've wrapped the promise returned by someFunThatReturnsAPromise()in a new promise, there's nothing that handles the return anyway so it wouldn't make any difference even if did return something.
此外,由于您已将返回的承诺包装someFunThatReturnsAPromise()在新的承诺中,因此无论如何都没有处理返回的内容,因此即使确实返回了某些东西也不会产生任何区别。
More importantly, the way you've wrapped a promise in a new promise is an anti-pattern. The new Promise()construct is only for dealing with asynchronous processes that aren't already promise based.
Since someFunThatReturnsAPromise()already return a promise, you don't need to wrap it in a new one, simply use the one you got!
For your example that would simply mean returning it:
更重要的是,在新承诺中包装承诺的方式是一种反模式。该new Promise()构造仅用于处理尚未基于 Promise 的异步进程。
既然someFunThatReturnsAPromise()已经返回了一个承诺,你不需要用一个新的包装它,只需使用你得到的一个!
对于您的示例,这仅意味着返回它:
return someFunThatReturnsAPromise()
If you want to do some processing of the data, such as only returning a part of the data (the statusproperty in the below example), you do that in a thencallback:
如果您想对数据进行一些处理,例如只返回一部分数据(status下面示例中的属性),您可以在then回调中执行此操作:
return someFunThatReturnsAPromise().then(data => data.status)
When you return in a thencallback, it will in turn return a new promise that gets resolved with the data you returned (unless you returned another promise, in which case it will resolve when that promise resolves).
This is how promises are designed to work, by chaining the asynchronous processes and their results.
当您在then回调中返回时,它会反过来返回一个新的承诺,该承诺会使用您返回的数据进行解析(除非您返回了另一个承诺,在这种情况下,当该承诺解析时它将解决)。
这就是 Promise 的工作方式,通过链接异步进程及其结果。
回答by marvel308
If you just want to return data, and reject incase of an error then you don't need a then()
如果您只想返回数据,并拒绝出现错误,那么您不需要 then()
return new Promise(function(resolve, reject)
{
someFunThatReturnsAPromise()
.then(data => resolve(data))
.catch(err => reject(err));
});
would be equivalent to
将相当于
return someFunThatReturnsAPromise()
unless you want to do some processing on data
除非你想对数据做一些处理

