Skip to content

Commit

Permalink
feat(aigle): add value and reason functions
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru03 committed Sep 2, 2017
1 parent 7f01c79 commit 74e0d5d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/aigle.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ class Aigle extends AigleCore {
return this._value instanceof CancellationError;
}

/**
* @return {*}
*/
value() {
return this._resolved === 1 ? this._value : undefined;
}

/**
* @return {*}
*/
reason() {
return this._resolved === 2 ? this._value : undefined;
}

/**
* @example
* const { CancellationError } = Aigle;
Expand Down
42 changes: 42 additions & 0 deletions test/lib/test.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,45 @@ parallel('#isCancelled', () => {
assert.strictEqual(promise.isCancelled(), false);
});
});

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

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

const value = Aigle.resolve(1).value();
assert.strictEqual(value, 1);
});

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

const value = new Aigle(() => {}).value();
assert.strictEqual(value, undefined);
});

it('should not return a value if a promise is rejected', () => {

const value = Aigle.reject(1).value();
assert.strictEqual(value, undefined);
});
});

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

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

const reason = Aigle.resolve(1).reason();
assert.strictEqual(reason, undefined);
});

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

const reason = new Aigle(() => {}).reason();
assert.strictEqual(reason, undefined);
});

it('should not return a reason if a promise is rejected', () => {

const reason = Aigle.reject(1).reason();
assert.strictEqual(reason, 1);
});
});

0 comments on commit 74e0d5d

Please sign in to comment.