Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...