-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref #35
- Loading branch information
Showing
5 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { invoker } from 'ramda'; | ||
|
||
/** | ||
* Composable shortcut for `Promise.then`. | ||
* The thenP function returns a Promise. It takes two arguments: a callback function for the success of the Promise | ||
* and the promise instance itself. | ||
* | ||
* @func thenP | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/2.8.0|v2.8.0} | ||
* @category Function | ||
* @sig (a -> Promise b | b) -> Promise b | ||
* @param {Function} onFulfilled A Function called if the Promise is fulfilled. This function has one argument, the fulfillment value | ||
* @param {Promise} promise Any Promise or Thenable object | ||
* @return {Promise} A Promise in the pending status | ||
* @see {@link RA.resolveP|resolveP}, {@link RA.rejectP|rejectP}, {@link RA.allP|allP} | ||
* @example | ||
* | ||
* const promise = Promise.resolve(1); | ||
* const add1 = v => v + 1; | ||
* | ||
* RA.thenP(add1, promise); // => Promise(2) | ||
*/ | ||
const thenP = invoker(1, 'then'); | ||
|
||
export default thenP; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { assert } from 'chai'; | ||
import sinon from 'sinon'; | ||
import * as R from 'ramda'; | ||
|
||
import * as RA from '../src/index'; | ||
|
||
describe('thenP', function() { | ||
context('when applied on Promise', function() { | ||
specify('should call then method with onFulfilled function', function() { | ||
const promise = RA.resolveP(1); | ||
const expected = RA.thenP(R.add(1), promise); | ||
|
||
assert.eventually.strictEqual(expected, 2); | ||
}); | ||
}); | ||
|
||
context('when applied on Thenable', function() { | ||
specify('should call then method with onFulfilled function', function() { | ||
const thenable = { then: fn => RA.resolveP(fn(1)) }; | ||
const expected = RA.thenP(R.add(1), thenable); | ||
|
||
assert.eventually.strictEqual(expected, 2); | ||
}); | ||
}); | ||
|
||
context('when applied on non-thenable', function() { | ||
specify('should throw TypeError', function() { | ||
assert.throws(() => RA.thenP(R.identity, {}), TypeError); | ||
}); | ||
}); | ||
|
||
it('should call `then` method on thenable', function(done) { | ||
const then = sinon.stub().returns(RA.resolveP(1)); | ||
const thenable = { then }; | ||
const expected = RA.thenP(R.identity, thenable); | ||
|
||
expected.then(() => { | ||
assert.isTrue(then.calledOnce); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should have arity of 2', function() { | ||
assert.strictEqual(RA.thenP.length, 2); | ||
}); | ||
|
||
it('should be curried', function() { | ||
assert.eventually.strictEqual(RA.thenP(R.identity, RA.resolveP(1)), 1); | ||
assert.eventually.strictEqual(RA.thenP(R.identity)(RA.resolveP(1)), 1); | ||
}); | ||
}); |