javascript node.js ramda.js

Handling asynchronous programming with Ramda

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

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?


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 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)
.catch(function(val){console.log("Error "+val)})