Skip to content

Instantly share code, notes, and snippets.

@delta-bravo
Created November 10, 2016 15:39
Show Gist options
  • Save delta-bravo/c71d65b539a079d0b978e6ecb98da085 to your computer and use it in GitHub Desktop.
Save delta-bravo/c71d65b539a079d0b978e6ecb98da085 to your computer and use it in GitHub Desktop.
var app = require('express')();
function SimplePromise(){
this.promise = new Promise();
}
SimplePromise.prototype.resolve = function(subj) {
for (var i = 0; i < this.promise.callbacks.length; i++) {
this.promise.callbacks[i](subj);
this.promise.callbacks.splice(i, 1);
i -= 1;
}
this.promise.subj = subj;
this.promise.resolved = true;
}
SimplePromise.prototype.reject = function(error) {
throw error;
}
function Promise() {
this.callbacks = [];
this.resolved = false;
this.subj = null;
}
Promise.prototype.then = function(callback) {
if(this.resolved) {
setTimeout(callback, 0, this.subj);
console.log('Resolved')
} else {
console.log(callback.toString());
this.callbacks.push(callback);
}
return this;
}
function runPromises() {
var simplePromise = new SimplePromise();
setTimeout(function() {
simplePromise.resolve('Ho-ho');
}, 500);
return simplePromise.promise;
}
app.get('/api', function(req, res) {
var promises = {};
runPromises().then(function(subj){
var theTime = new Date();
promises.p1 = {subject: subj, timeStamp: theTime};
console.log(subj + ' ' + theTime);
}).then(function(subj) {
var theTime = new Date();
promises.p2 = {subject: subj, timeStamp: theTime};
console.log(subj + ' ' + theTime);
res.status(200).json(promises)
});
})
app.listen('8989');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment