admin管理员组

文章数量:1178529

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 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.

Share Improve this question asked Nov 2, 2015 at 21:10 BBSBBS 1,4092 gold badges13 silver badges28 bronze badges 9
  • 2 Sound like you want to lift the equals function - first in the promise monad and then in the function applicative; unfortunately Ramda can only lift for lists. – Bergi Commented Nov 2, 2015 at 21:16
  • So you want a function ??? :: (obj -> Promise<number>) -> (obj -> number) -> (obj -> Promise<boolean>)? – Bergi Commented Nov 2, 2015 at 21:18
  • @Bergi: Ramda's documentation is not quite up to snuff here, but lift does work on arbitrary Applicative Functors. – Scott Sauyet Commented Nov 2, 2015 at 21:24
  • So it should be something like liftN(2, flip(compose(map, equals))) assuming that your functions implement Applicative like this and your Promises implement Functor – Bergi Commented Nov 2, 2015 at 21:44
  • That is helpful. I understand conceptually that we need to change the equals to ??? :: (obj -> Promise<number>) -> (obj -> number) -> (obj -> Promise<boolean>) but I am not sure syntactically how lift should work. – BBS Commented Nov 2, 2015 at 22:17
 |  Show 4 more comments

5 Answers 5

Reset to default 14

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.

You can create custom compose: https://gist.github.com/ehpc/2a524b78729ee6b4e8111f89c66d7ff5

本文标签: javascriptHandling asynchronous programming with RamdaStack Overflow