Skip to content

Commit

Permalink
structured for npm
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrans committed Sep 23, 2013
1 parent f843d3a commit 556aa69
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 51 deletions.
32 changes: 0 additions & 32 deletions lib/all.js

This file was deleted.

7 changes: 7 additions & 0 deletions lib/index.js
@@ -0,0 +1,7 @@
module.exports = require('./promise');

var tools = require('./tools');

for (var key in tools) {
module.exports[key] = tools[key];
}
21 changes: 9 additions & 12 deletions lib/promise.js
@@ -1,3 +1,4 @@
// Bind an objects method to the object
function bindMethod(obj, name){
var method = obj[name];
obj[name] = function() { return method.apply(obj, arguments); };
Expand All @@ -13,8 +14,9 @@ function isThenable(x) {

function Branch(onFulfilled, onRejected) {
bindMethod(this, 'then');

this.on = { fulfilled: onFulfilled, rejected: onRejected };
this.promise = Promise.empty;
this.promise = new Promise();
}

Branch.prototype.transition = function(state, valueOrReason) {
Expand All @@ -31,25 +33,17 @@ Branch.prototype.transition = function(state, valueOrReason) {
}
};

Branch.prototype.then = function(onFulfilled, onRejected) {
if (this.promise === Promise.empty) {
this.promise = new Promise();
}
return this.promise.then(onFulfilled, onRejected);
};

function Promise(rvalue) {
bindMethod(this, 'resolve');
bindMethod(this, 'reject');
bindMethod(this, 'then');

this.pendingBranches = [];
this.state = 'pending';

arguments.length && this.resolve(rvalue);
}

Promise.empty = {resolve: function(){}, reject: function(){}};

Promise.prototype.scheduleBranchTransitions = function() {
var branches = this.pendingBranches;
var state = this.state;
Expand Down Expand Up @@ -80,6 +74,7 @@ Promise.prototype.resolve = function(rvalue) {

Promise.prototype.reject = function(reason) {
this.transition('rejected', reason);
throw reason;
};

Promise.prototype.then = function(onFulfilled, onRejected) {
Expand All @@ -88,11 +83,13 @@ Promise.prototype.then = function(onFulfilled, onRejected) {
if(this.state !== 'pending') {
this.scheduleBranchTransitions();
}
return {then: branch.then};
return {then: branch.promise.then};
};

module.exports = function(fn) {
return { then: new Promise({ then: fn }).then }
return new Promise({ then: fn });
};

module.exports.Promise = Promise;
module.exports.isFunction = isFunction;
module.exports.isThenable = isThenable;
58 changes: 58 additions & 0 deletions lib/tools.js
@@ -0,0 +1,58 @@
var promise = require('./promise');

function when(value) {
return promise.isThenable(value) ?
value :
new promise.Promise(value);
}
exports.when = when;

function all(promises) {

return promise(function(resolve, reject) {

var pendingCount = promises.length;
var values = [];

function checkDone() {
if(pendingCount === 0) {
resolve(values);
}
}

function onFulfilled(i) {
return function(value) {
values[i] = value;
--pendingCount;
checkDone();
}
}

for(var i = 0; i < promises.length; i++) {
when(promises[i]).then(onFulfilled(i), reject);
}

checkDone();
});
}

exports.all = all;



exports.wrapAsyncSimple = function wrapAsyncSimple(fn) {
return function wrappedAsyncSimple() {
var args = Array.prototype.slice.call(arguments);
var _this = this;

return promise(function (resolve) {
args.push(function callback() {
resolve.apply(null, arguments);
});
fn.apply(_this, args);
});

};
};


11 changes: 4 additions & 7 deletions package.json
@@ -1,14 +1,11 @@
{
"name": "iou",
"name": "zz",
"version": "0.0.0",
"description": "TBA",
"main": "later.js",
"scripts": {
"test": "foo"
},
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/dbrans/iou"
"url": "https://github.com/dbrans/promisejs"
},
"keywords": [
"foo"
Expand All @@ -17,6 +14,6 @@
"license": "BSD",
"gitHead": "1cf64908ca1e32a8917f140adf8ae335c1dbf522",
"bugs": {
"url": "https://github.com/dbrans/iou/issues"
"url": "https://github.com/dbrans/promisejs/issues"
}
}

0 comments on commit 556aa69

Please sign in to comment.