Created
October 10, 2015 16:51
-
-
Save winsonwq/a3ae8787df9e790cb0e2 to your computer and use it in GitHub Desktop.
functional practice - promise map
This file contains hidden or 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
const R = require('ramda'); | |
const Promise = require('es6-promise').Promise; | |
const pMap = require('./promise-map'); | |
function get(method) { | |
return Promise[method]({ | |
addresses: [ | |
{ street: 'street a' }, | |
{ street: 'street b' }, | |
{ street: 'street c' } | |
] | |
}) | |
} | |
const firstStreet = R.compose(R.nth(0), R.path(['addresses'])); | |
const secondStreet = R.compose(R.nth(1), R.path(['addresses'])); | |
function getFirstStreet() { | |
return get().then(firstStreet); | |
} | |
const getFirstStreet2 = R.compose(pMap(firstStreet, secondStreet), get); | |
const getFirst2Streets = R.compose( | |
pMap(R.map(firstStreet), secondStreet), | |
Promise.all.bind(Promise), | |
R.call(R.useWith(Array, get, get)) | |
); | |
const a = getFirst2Streets('resolve', 'resolve') | |
.then(console.log.bind(console)) | |
.catch(console.log.bind(console)); |
This file contains hidden or 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
const R = require('ramda'); | |
const Promise = require('es6-promise').Promise; | |
module.exports = R.curry(function(resolve, reject, promise) { | |
if (promise.constructor == Promise) { | |
return promise.then(resolve, reject || R.identity); | |
} else { | |
return Promise.resolve(promise); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment