Skip to content

Instantly share code, notes, and snippets.

Created February 7, 2011 06:02
Show Gist options
  • Save unscriptable/814052 to your computer and use it in GitHub Desktop.
Save unscriptable/814052 to your computer and use it in GitHub Desktop.
A minimalist implementation of a javascript promise
// (c) copyright / John Hann
// License MIT
// For more robust promises, see
function Promise () {
this._thens = [];
Promise.prototype = {
/* This is the "front end" API. */
// then(onResolve, onReject): Code waiting for this promise uses the
// then() method to be notified when the promise is complete. There
// are two completion callbacks: onReject and onResolve. A more
// robust promise implementation will also have an onProgress handler.
then: function (onResolve, onReject) {
// capture calls to then()
this._thens.push({ resolve: onResolve, reject: onReject });
// Some promise implementations also have a cancel() front end API that
// calls all of the onReject() callbacks (aka a "cancelable promise").
// cancel: function (reason) {},
/* This is the "back end" API. */
// resolve(resolvedValue): The resolve() method is called when a promise
// is resolved (duh). The resolved value (if any) is passed by the resolver
// to this method. All waiting onResolve callbacks are called
// and any future ones are, too, each being passed the resolved value.
resolve: function (val) { this._complete('resolve', val); },
// reject(exception): The reject() method is called when a promise cannot
// be resolved. Typically, you'd pass an exception as the single parameter,
// but any other argument, including none at all, is acceptable.
// All waiting and all future onReject callbacks are called when reject()
// is called and are passed the exception parameter.
reject: function (ex) { this._complete('reject', ex); },
// Some promises may have a progress handler. The back end API to signal a
// progress "event" has a single parameter. The contents of this parameter
// could be just about anything and is specific to your implementation.
// progress: function (data) {},
/* "Private" methods. */
_complete: function (which, arg) {
// switch over to sync then()
this.then = which === 'resolve' ?
function (resolve, reject) { resolve && resolve(arg); } :
function (resolve, reject) { reject && reject(arg); };
// disallow multiple calls to resolve or reject
this.resolve = this.reject =
function () { throw new Error('Promise already completed.'); };
// complete all waiting (async) then()s
var aThen, i = 0;
while (aThen = this._thens[i++]) { aThen[which] && aThen[which](arg); }
delete this._thens;
Copy link

Astute coders will notice [at least] two problems with this minimalist implementation:

  1. The public methods are rewritten once the promise is resolved or rejected. This is done for speed and code size. However, if outside code were to obtain a reference to one of these methods, the reference would become useless since it would still point at the old method.
  2. If a resolve callback supplied in any then() call were to modify the resolved value, the modified value -- not the original value -- would be passed to subsequent resolve callbacks. Outside of a constrained environment, this could cause hard-to-debug errors.

If used in a constrained situation (e.g. as a utility inside a module), then this is a really fast, really lightweight implementation.

Copy link

Added comments

Copy link

No longer fails when a resolve or reject parameter are missing when calling the replaced (sync) then(). Thanks Brian Cavalier!

Copy link

uhop commented Feb 7, 2011

What if the resolve value is a Promise itself? In general: what is the way to chain promises rather then broadcast their resolution?

Copy link

Hey uhop!

This is a minimalist promise implementation. You're way too advanced for this gist, man! :)
I was thinking of creating a couple of flavors of promises, from this simple one all the way up to something like dojo.Deferred. :)

That's when promises get really interesting!

-- J

Copy link

Here's a closure-ified version, too if someone wants that flavor :) I haven't updated it to be current with your latest, but pretty close.

Copy link

What is the license of the code ?

Copy link

Hello @cedricpinson.

Does MIT License work for you? If so, I'll add a note to the source.

Btw, if you're looking for CommonJS Promises/A promises, you should check out The promise in this gist is not compliant because it does not allow chained promises to safely mutate the resolved value. The work here helped inspire the when.js project, which is compliant with Promises/A. (Note: jQuery.Deferred is not Promises/A-compliant either even though they claim it is.)


-- John

Copy link

Ok thanks for the pointer I was looking to put code like that in my library, so if you tell me when.js is more robust/compliant, I will check it directly to add it into my lib. Thanks for the quick answer

Copy link

Hey @cedricpinson,

Yep, when.js is Promises/A compliant, and includes a few extras (when.all/any/some and when.chain). The unit test suite is decent, and it's also used inside, so is getting a good bit of real world battle testing, too. If you try out when.js, feel free to hit me with feedback and questions.

Copy link

What an excellent implementation of promise. Thanks for sharing! @unscriptable

Copy link

j-norman commented Apr 8, 2014

You've done a few wonderful things in this to keep the code incredibly short, and yet it is still very readable. Props, and thank you.

Copy link

jmas commented Apr 28, 2015

What about catch() for wrap errors throws?

Copy link

sukhmeet2390 commented Oct 25, 2016

Promises can only be resolved by function which has created it. How does it takes care of that.

function something(){
  var p = new Promise() ;
 return p;

var p = something();

Copy link

Yours are more like a deferred promise...
So i wanted to try write a similar a+ promise and only try to do the bare minimum

This is what i ended up with when it was minified (hand crafted):

TinyPromise = (h, f = [], b = -1, g, c, l) => (
  l = d => {for (g = d; c = f.shift();) c[b] && c[b](d)},
  // execute the handler(resolve, reject)
  h(d => l(d, b = 0), d => l(d, b = 1)),
  // return a thenable (non chainable)
  {then(...d){ ~b ? d[b] && d[b](g) : f.push(d)} }


TinyPromise((resolve, reject) => resolve(3)).then(console.log, console.error)

Someone wants to try to beat this or try to make it a little better? 😜
Goal is not to make it complete spec compatible but to make it do just what you need

Copy link

@jimmywarting I think it will be better to post the uncompressed version so you have better feedback about this

Copy link

matthieusieben commented Aug 8, 2017

How about one that is chainable, that returns an actual instance ?

function P (cb) {
  var q = [], v, u, ok, complete = function (m, r) {
    if (q) {
      var i = -1, l = q;
      ok = !m; v = r; q = null;
      while (++i < l.length) l[i][m](v);

  cb(complete.bind(u, 0), complete.bind(u, 1));

  this.then = then;
  this.catch = then.bind(u, u)

  function then(success, error) {
    return new P(function (resolve, reject) {
      if (q) q.push([ done.bind(u, success), done.bind(u, error) ]);
      else done(ok ? success : error);

      function done (cb) { try {
        var val = cb ? cb(v) : u;
        if (val && val.then) val.then(resolve, reject);
        else (cb || ok ? resolve : reject)(val);
      } catch (e) { reject(e) } }

Copy link

souparno commented Nov 17, 2018

i guess a much more simpler implementation could be this

var Promise = function() {
    var this._callBacks = [];

    this.then = function(fn) {
        return this;

    this.resolve = function(data, err) {
        this._callBacks[0].call(this, data, err);

function readPromise(filename) {
    var promise = new Promise();

    fs.readFile(filename, function(err, data) {
        promise.resolve(data, err);
    return promise;

    .then(function(data, err) {
        //-- do something here -- //
        //-- resolve promise with this.resolve(data, err) -- //
    .then(function(data, err) {
        //-- do something here -- //

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