承诺JS中的决心和回报之间的区别 [英] Difference between resolve and return in promise JS
问题描述
var p1 = new Promise (function (res, rej){
res(42);
}).then((result) => {return result;});
**如果我有** 返回结果
,
**If I have ** return result
,
此承诺是否已解决? 已解决的承诺是什么意思?
is this promise resolved or not? What does a "resolved promise" mean?
推荐答案
Java Script是一种单线程语言。这简化了大多数任务;但是,这意味着必须在回调函数中处理异步任务。 承诺是一种面向对象的类型回调提供比简单回调函数更强大的功能。
Java Script is a single threaded language. This simplifies most tasks; but, it means asynchronous tasks must be handled in a callback function. A Promise is an object oriented type of callback that offers greater functionality than a simple callback function.
已解决承诺意味着然后将调用promise对象的函数。在您的示例中,承诺已得到解决。
A resolved promise means that the then function of the promise object will be called. In your example, the promise has been resolved.
已拒绝承诺意味着 catch 函数将被调用。
A rejected promise means that the catch function of the promise object will be called.
在然后功能,允许链。然后,每个结果都可以在将结果传递给链中的下一个承诺之前更改或操纵结果。
Returning a result in a then function, allows for chaining. Each then result can change or manipulate the result for before passing it on to the next promise in the chain.
在您的示例中,您解决了第一个承诺,然后返回了链中的下一个承诺的结果是你没有处理得如此有效,返回的结果什么都不做。
In your example, you resolved the first promise and then returned a result for the next promise in the chain which you don't handle so effectively the returned result does nothing.
这篇关于承诺JS中的决心和回报之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!