

Promises: resolve is not the opposite of reject
source link: https://jakearchibald.com/2014/resolve-not-opposite-of-reject/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Promises: resolve is not the opposite of reject
Posted 13 March 2014 and still refusing to do promise-based punsWhen I first started working with promises I had the overly simplistic view that passing a value into reject
would mark the promise as "failed", and passing a value into resolve
would mark it as "successful". However, the latter isn't always true.
new Promise(function(resolve, reject) {
resolve(something);
}).then(function() {
console.log("Yey");
}, function() {
console.log("Boo");
});
Even though we aren't calling reject()
, the rejection callback console.log("Boo")
will be called if either:
something
is not defined, resulting in an error being thrown, which is caught by the promise and turned into a rejection, orsomething
is a promise that rejects
new Promise(function(resolve, reject) {
resolve(Promise.reject());
}).catch(function() {
// This is called
});
This is a good thing, as it behaves the same as Promise.resolve()
and the return value from callbacks:
var promise1 = Promise.resolve(Promise.reject());
var promise2 = Promise.resolve().then(function() {
return Promise.reject();
});
var promise3 = Promise.reject().catch(function() {
return Promise.reject();
});
All promises above are rejected. When you resolve a value with a "then" method, you're deferring the resolution to the eventual non-promise value.
In Practice
You can resolve a value without worrying if it's a value, a promise, or a promise that resolves to a promise that resolves to a promise etc etc.
function apiCall(method, params) {
return new Promise(function(resolve, reject) {
if (!method) {
throw TypeError("apiCall: You must provide a method");
}
var data = {
jsonrpc: "2.0",
method: method
}
if (params) {
data.params = params;
}
resolve(postJSON('/my/api/', data));
});
}
Now apiCall
will reject if method
isn't provided, or if postJSON
rejects for whatever reason. You've safely handed off the resolution of the promise onto postJSON
.
Further reading
Recommend
-
99
Writing asynchronous JavaScript without using the Promise object is a lot like baking a cake with your eyes closed. It can be done, but it's gonna be
-
49
only-last-promise Resolve or reject only last Promise. Useful if you want to "abort" already running async operations started with debounced input event. Install npm install only-last-...
-
8
After getting the hang of async programming in javascript, you might quickly find you need to wait for multiple async tasks to happen. Enter Promise.all! A Single Promise To start out, let’s do a simple fetch requ...
-
19
in which we coin a term which is the opposite of deprecate Earlier this month I published the results of a s...
-
16
cubicaltt/opposite.ctt at master · mortberg/cubicaltt · GitHubPermalink 35 lines (25 sloc)...
-
13
Don’t miss what’s happeningPeople on Twitter are the first to know.
-
7
Using Resolve -Promises in Angular 2 Http Services Reading Time: 2 minutesYou have a huge list of users that you hav...
-
10
Crypto DecodedU.S. should do ‘exact opposite’ of China on crypto, says Andreessen Horowitz’s Katie HaunPublished Wed, Sep 29 20215:22 PM EDTUpda...
-
16
U.S. should do the ‘exact opposite’ of China on crypto: Venture Capitalist Katie Haun October 4, 2021 by Editor's Desk ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK