question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage of curry with promises

See original GitHub issue

Hi guys,

i am in a condition that i cannot figure out.

I want to curry a function that will return a promise. Let suppose there is something like:

const getUser = (token, id) => Promise.resolve({id: 1});

Now i think that can get a curried version of this function

const S = require('sanctuary');
const getUserAuthenticated = S.curry2(getUser)("Bearer test")

So now i get use the getUserAuthenticated function and i can map it when i need to, but sadly i get a type error because the promise is not a correct type.

getUserAuthenticated(1).then(...)
TypeError: Unrecognized value
           
curry2 :: ((a, b) -> c) -> a -> b -> c
                     ^
                     1
        
1)  [object Promise] :: (no types)
             
The value at position 1 is not a member of any type in the environment.

The environment contains the following types:                                                                                            
                                                                                                                                         
  - Function                                                                                                                             
  - Arguments                                                                                                                            
  - Array d
...

Why there is a limitation with curry and types?

Thanks Walter

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
efristercommented, Jul 11, 2018

Hi guys,

what’s the preferred way to do async operations when working with Sanctuary: using Promises and defining a custom type as above, or using Fluture? Or something entirely different?

1reaction
davidchamberscommented, Jul 3, 2018

Here’s a working example to get you unstuck, @wdalmut:

'use strict';

const {create, env} = require ('sanctuary');
const $ = require ('sanctuary-def');

//    PromiseType :: Type -> Type -> Type
const PromiseType = $.BinaryType
  ('my-package/Promise')
  ('https://example.com/my-package#Promise')
  (x => Object.prototype.toString.call (x) === '[object Promise]')
  (p => [])
  (p => []);

const S = create ({
  checkTypes: true,
  env: env.concat ([PromiseType ($.Unknown) ($.Unknown)]),
});

console.log (S.I (Promise.resolve (42)));

@davidchambers please correct me if I’ve gotten anything wrong.

Your answer was spot on, Gabe! I think providing a Promise type constructor is a good idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cleaning up Promise Chains with Partial and Curried Functions
We already have promise-returning helper methods to validate the user data, fetch the api secret, and create a new project.
Read more >
JavaScript Async - Returning Promises - I Programmer
The final way of doing the job is to use bind to curry the delay function. The bind function returns another function with...
Read more >
Typescript Promise curry function when using bind
The problem here is that the close function must return the value it was bound with, which, in this case is possibly of...
Read more >
Callback currying, and futures (or a preface to Promises)
I have been diving into the different patterns that can be used to organize functional code. One pattern being ` curry ` ,...
Read more >
Currying in JavaScript ES6 - Bene Studio
someAsyncOperation is a function that returns a promise that rejects if the path is not a string otherwise it resolves. On line 16...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found