I am looking at handling functions that return promises with Ramda functions other then pipeP. I am trying to compare functions (one of which returns a promise) with equals like this:
getSectionFromDb :: obj -> promise
getSectionFromData :: obj -> number
R.equals(
getSectionFromDb,
getSectionFromData
)
There are two factors at play here. First R.equals will not evaluate functions, but the bigger issue is that I'm comparing a promise to a number.
Is there a functional way of doing this kind of stuff (I know the functions are not referential transparent, but there must be a way of dealing with io)? Is there a Ramda way of doing this?
Thanks.
I know, the question is old. But ramda has some cool functions to compose Promise-returning functions: pipeP and composeP.
Also take a look into regular compose (pipe) and it's Kleisli implementation composeK (pipeK). They allow to work with algebraic structures like Future or Task, which look the same as Promise, but lazy-evaluated.
You can use Promise.resolve
to "wrap" a value in a promise.
getSectionFromDataPromise :: obj -> promise
getSectionFromDataPromise = R.pipe(getSectionFromData , (val) => Promise.resolve(val))
This way you can promote (lift) any function that returns a normal value to a one that returns a promise.
Lifting is an essential concept in FP. You can view Array.map
as a function that lifts a function that transforms a value to a function that transforms an array of values.
You can use Promise.all
to write a function that compares promises and (for example) throws an error if they are not equal.
function promiseEquals (f1, f2) {
return Promise.all([f1(), f2()]).then(function(vals) {
if(!R.equals(vals[0], vals[1])) {throw "The values aren't equal"}
return vals[0]
})
}
Lastly you can combine the two:
promiseEquals(getSectionFromDataPromise, getSectionFromDb)
.then(function(val){
console.log(val)
})
.catch(function(val){console.log("Error "+val)})
pipeP
and composeP
got deprecated.
Create pipeWithPromise
which accepts an array of promises or functions.
var pipeWithPromise = R.pipeWith((fun, previousResult) => (previousResult && previousResult.then) ? previousResult.then(fun) : fun(previousResult));
var tasks = [/* sync func */ $ => $ + '1', /* async func */ async $ => await $ + '2'];
var result = await pipeWithPromise(tasks)('state');
// result = 'state12';
It's not ramda, but it'll do what you want
const { eq } = require('rubico')
/*
getSectionFromDb :: obj -> promise
getSectionFromData :: obj -> number
*/
eq(
getSectionFromDb,
getSectionFromData
)({...}) // => Promise { true }
rubico's eq will resolve promises under the hood, so the promise you get from getSectionFromDb
will be resolved before it is compared with the number from getSectionFromData
. The return value will be a Promise of a boolean that you'll have to resolve elsewhere, however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With