Skip to content

Instantly share code, notes, and snippets.

@jpina
Created November 25, 2015 11:00
Show Gist options
  • Save jpina/0076db061345785d7070 to your computer and use it in GitHub Desktop.
Save jpina/0076db061345785d7070 to your computer and use it in GitHub Desktop.
00 Promise API Reference

Promise API Reference

Static Methods

Promise.resolve(promise);

Returns promise (only if promise.constructor == Promise)

Promise.resolve(obj);

Make a promise that fulfills to obj. in this situation.

Promise.reject(obj);

Make a promise that rejects to obj. For consistency and debugging (e.g. stack traces), obj should be an instanceof Error.

Promise.all(array);

Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects. Each array item is passed to Promise.resolve, so the array can be a mixture of promise-like objects and other objects. The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value.

Constructor

new Promise(function(resolve, reject) {});

resolve(obj)

Your promise is fulfilled with obj

reject(obj)

Your promise is rejected with obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error. Any errors thrown in the constructor callback will be implicitly passed to reject().

Instance Methods

promise.then(onFulfilled, onRejected)

onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. Both callbacks have a single parameter, the fulfillment value or rejection reason. "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. If an error is thrown in the callback, the returned promise rejects with that error.

promise.catch(onRejected)

Sugar for promise.then(undefined, onRejected)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment