Skip to content

Commit

Permalink
feat(aigle): add inspection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru03 committed Aug 24, 2017
1 parent 29646c1 commit 52209fa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/aigle.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ class Aigle extends AigleCore {
this._execute(executor);
}

/**
* @return {string}
*/
toString() {
return '[object Promise]';
}

/**
* @param {Function} onFulfilled
* @param {Function} [onRejected]
Expand Down Expand Up @@ -99,6 +92,41 @@ class Aigle extends AigleCore {
return addAigle(this, new Aigle(INTERNAL), handler, handler);
}

/**
* @return {string}
*/
toString() {
return '[object Promise]';
}

/**
* @return {boolean}
*/
isPending() {
return this._resolved === 0;
}

/**
* @return {boolean}
*/
isFulfilled() {
return this._resolved === 1;
}

/**
* @return {boolean}
*/
isRejected() {
return this._resolved === 2;
}

/**
* @return {boolean}
*/
isCancelled() {
return this._value instanceof CancellationError;
}

/**
* @example
* const { CancellationError } = Aigle;
Expand Down
2 changes: 2 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
env:
mocha: true
parserOptions:
ecmaVersion: 8
rules:
Expand Down
30 changes: 30 additions & 0 deletions test/lib/test.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,33 @@ parallel('#toString', () => {
assert.strictEqual(promise.toString(), '[object Promise]');
});
});

parallel('#isPending', () => {

it('should return true if a promise is pending', () => {

const promise = new Aigle(() => {});
assert.strictEqual(promise.isPending(), true);
});

it('should return false if a promise is fulfilled', () => {

const promise = Aigle.resolve();
assert.strictEqual(promise.isPending(), false);
});
});

parallel('#isFulfilled', () => {

it('should return false if a promise is pending', () => {

const promise = new Aigle(() => {});
assert.strictEqual(promise.isFulfilled(), false);
});

it('should return true if a promise is fulfilled', () => {

const promise = Aigle.resolve();
assert.strictEqual(promise.isFulfilled(), true);
});
});

0 comments on commit 52209fa

Please sign in to comment.