Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

q.js: difference between resolve() and fulfill()

I'm still unclear on the difference between calling a resolver's resolve() vs fulfill()? I see both the functions and the terms "resolve a promise" and "fulfill a promise" batted around a lot.

When should I be using each?

like image 229
alecf Avatar asked Aug 16 '13 19:08

alecf


People also ask

What does resolve do in JS?

resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.

What is a fulfilled promise Javascript?

The Promise object supports two properties: state and result. While a Promise object is "pending" (working), the result is undefined. When a Promise object is "fulfilled", the result is a value. When a Promise object is "rejected", the result is an error object.

What does promise State fulfilled mean?

Fulfilled is a state of a Promise. It means that the promise has been resolved and now has its resolved value (using the internal resolve function). The operation represented by the promise has been completed successfully.

Is Promise resolve same as return?

resolve("aaa") is the same as return Promise. resolve(Promise. resolve("aaa")) - since resolve is idempotent calling it on a value more than once has the same result.


1 Answers

You should use resolve. deferredPromise.resolve(nextPromise) means that anything waiting for deferredPromise will now wait for nextPromise. If nextPromise is not a promise at all, it gets turned into a fulfilled promise which goes on to inform anything waiting for it that the value has become available.

The fulfill method is a bad idea that will be deprecated and eventually go away entirely. fulfill is semantically equivalent to resolve in all useful cases. It’s only reason to exist is that deferredPromise.fulfill(value) is easier for humans to interpret than deferredPromise.resolve(value), since resolve is overloaded to handle both nextPromise and finalValue.

The problem with fulfill existing at all is that deferredPromise.fulfill(rejectedPromise) is semantically paradoxical.

like image 113
Kris Kowal Avatar answered Sep 18 '22 15:09

Kris Kowal