Custom promise object

Simple implementation of a promise

by Malin Jayakody

JavaScript

'use strict';
var promise = function() {
    this.isDone = false;
    this.callbacks = [];
    this.done = function(callback) {
        if (this.isDone) {
            callback();
        } else {
            this.callbacks.push(callback);
        }
    };
    this.resolve = function() {
        this.isDone = true;
        jQuery.each(this.callbacks, function(index, callback) {
            callback();
        });
    };
};

function test() {
    var x = new promise();
    x.done(function() {
        alert('tested!');
    });
    alert('Resolve?');
    x.resolve();
}

test();