From 556aa6990c94158a9261ad67b1d0c6437215f5d8 Mon Sep 17 00:00:00 2001 From: Derek Brans Date: Sun, 22 Sep 2013 17:35:02 -0700 Subject: [PATCH] structured for npm --- lib/all.js | 32 ---------------------------- lib/index.js | 7 ++++++ lib/promise.js | 21 ++++++++---------- lib/tools.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 11 ++++------ 5 files changed, 78 insertions(+), 51 deletions(-) delete mode 100644 lib/all.js create mode 100644 lib/index.js create mode 100644 lib/tools.js diff --git a/lib/all.js b/lib/all.js deleted file mode 100644 index 35bd1f8..0000000 --- a/lib/all.js +++ /dev/null @@ -1,32 +0,0 @@ -var promise = require('./promise'); - -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++) { - promises[i].then(onFulfilled(i), rej); - } - - checkDone(); - }); -}; - - diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..4aa03f6 --- /dev/null +++ b/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]; +} \ No newline at end of file diff --git a/lib/promise.js b/lib/promise.js index 90839b4..83e5479 100644 --- a/lib/promise.js +++ b/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); }; @@ -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) { @@ -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; @@ -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) { @@ -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; \ No newline at end of file diff --git a/lib/tools.js b/lib/tools.js new file mode 100644 index 0000000..d8bfb36 --- /dev/null +++ b/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); + }); + + }; +}; + + diff --git a/package.json b/package.json index 506d5e2..52a7987 100644 --- a/package.json +++ b/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" @@ -17,6 +14,6 @@ "license": "BSD", "gitHead": "1cf64908ca1e32a8917f140adf8ae335c1dbf522", "bugs": { - "url": "https://github.com/dbrans/iou/issues" + "url": "https://github.com/dbrans/promisejs/issues" } }