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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With