Skip to content

Commit

Permalink
Merge pull request node-modules#1 from node-modules/promise
Browse files Browse the repository at this point in the history
Promise
  • Loading branch information
fengmk2 committed Sep 29, 2015
2 parents 7b93042 + 99de679 commit 9daa77d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "0.6"
- "0.8"
- "0.10"
- "1"
- "2"
- "3"
- "4"
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ function ready(flagOrFunction) {
this._ready = !!this._ready;
this._readyCallbacks = this._readyCallbacks || [];

if ('function' === typeof(flagOrFunction)) {
if (arguments.length === 0) {
// return a promise
// support `this.ready().then(onready);` and `yield this.ready()`;
return new Promise(function (resolve, reject) {
if (this._ready) {
return resolve();
}
this._readyCallbacks.push(resolve);
}.bind(this));
} else if ('function' === typeof(flagOrFunction)) {
this._readyCallbacks.push(flagOrFunction);
} else {
this._ready = !!flagOrFunction;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
},
"dependencies": {},
"devDependencies": {
"mocha": "~1.12.0",
"co-mocha": "~1.1.2",
"mocha": "~2.3.3",
"should": "~0.6.3"
},
"scripts": {
"test": "mocha"
"test": "mocha --require co-mocha"
},
"repository": {
"type": "git",
Expand Down
22 changes: 21 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,24 @@ describe('ready', function() {
done();
}, 10);
});
});
});

describe('promise', function () {
var someClass = new SomeClass();
it('should resolve after ready', function (done) {
someClass.ready().then(function () {
someClass.ready().then(done);
});
someClass.ready(true);
});
});

describe('generator', function () {
var someClass = new SomeClass();
it('should work with co', function* () {
setTimeout(function () {
someClass.ready(true);
}, 100);
yield someClass.ready();
});
});

0 comments on commit 9daa77d

Please sign in to comment.