最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between resolve and return in promise JS - Stack Overflow

programmeradmin2浏览0评论
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?

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?

Share Improve this question asked Jan 3, 2017 at 0:53 user7361276user7361276 2292 gold badges4 silver badges11 bronze badges 1
  • Maybe this question is of help to you, having a seemingly similar misunderstanding: Why does the Promise constructor require a function that calls 'resolve' when complete, but 'then' does not - it returns a value instead?. Also, regarding the second question, see What is the correct terminology for javascript promises – Bergi Commented Jan 3, 2017 at 13:59
Add a comment  | 

3 Answers 3

Reset to default 9

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.

A resolved promise means that the then function of the promise object will be called. In your example, the promise has been resolved.

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.

Before asking about promises, you should consider what the function

result => result

which is the shorter way of writing your

(result) => { return result; }

actually does. It's called the identity function, which means it returns exactly what was passed in.

As a then handler, then, it means exactly nothing--it's a no-op. So the promise returned by then is in exactly the same state, with exactly the same value (if any), as the promise on which you called then.

Consider closely what promise.then(fn) does. It creates a new promise. As long as promise remains pending, then that new promise also remains pending. If and when promise is fulfilled, it calls fn with the fulfilled value as a parameter, executes fn, and (all else being equal) fulfills the new promise with the returned value from fn. If and when promise is rejected, on the other hand, fn is not called, and assuming no second parameter has been passed to then, then the new promise is placed in a rejected state with the same rejection reason--in essence, in this case, the promise can be considered to be "passed through".

So in the case of

promise.then(identity)

where identity is result => result, when promise fulfills with some value v, then identity is called with a parameter of v, returns the same value v, and fulfills the new promise created by then with that same value--in other words, it creates a new promise which has exactly the same status and value as promise. To put it a different way, it does nothing--or rather, merely creates a new promise identical to the one on which then was called.

In your case:

var p1 = new Promise (function (res, rej){
   res(42); })
.then((result) => {return result;});

is this promise resolved or not? What does a "resolved promise" mean?

The promise created by calling the promise constructor (new Promise) is immediately fulfilled with the value 42, by virtue of calling res(42). (You could also have written Promise.resolve(42), which would mean exactly the same thing.) The then creates a second promise. Since the first promise, on which then is being called, is already fulfilled, the then handler is immediately invoked with the parameter (result) of 42. It returns that same value, so therefore the second promise, the one created by then is then fulfilled with that value of 42. So p1 at that point is a fulfilled promise with a value of 42.

p1.then(v => console.log(v))

You will see 42 logged to the console.

There is nothing particularly magical about any of the above. It all derives from the basic principles of promises: their possible statuses, how to create them if you need to, and the meaning of then. A good tutorial or document such as this one should get you on the right track.

The promise is resolved with the value result. In the snippet you wrote, the then block doesn't change anything, as it makes the promise to resolve with the result value, just as it would be without the return statement and without the then block.

However, if you would return a different value, then the promise would resolve with that value. And if you would return another promise inside this then, you could "chain" the blocks, that is you could write a next 'then' block which would receive the resolved value of the returned promise in the former then block.

A resolved promise means, that the code handled by the promise is done, and the code in the callback passed to the then method is executed with the resolved value passed in.

发布评论

评论列表(0)

  1. 暂无评论