0

It's a bite of broken code:

async function foo() {
  return maybeMonad
    .map((resultMonad) => resultMonad
      .matchWith({
        Error: (errorMonad) => errorMonad,
        Ok: (okMonad) => okMonad
          .map(async (context) => {
            let res = await apiRequest(context.token);
            context.data = res.data;
  
            return context;
          }),
      }));
}


await foo() // => Maybe(Promise)

The idea is to get some value from context, make a request and return context with supplemented data. But it returns a Promise in pending status. What I'm doing wrong?

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
HAGer
  • 21
  • 5
  • Promises are in pending status until they resolve, so what's your problem? Also how are you getting Maybe(Promise) out of an async function? They implicitly wrap their return value in a Promise. – Jared Smith Dec 18 '20 at 12:56
  • apiRequest resolves OK, but foo returns maybe with unresolved promise instead maybe with result – HAGer Dec 18 '20 at 13:02
  • 1
    I think we're going to need a little more info about what you're actually trying to accomplish here...I'm not an *expert* in folktale/fantasyland but unless I'm completely mistaken your usage looks off. You have a lot of closure variables here that don't square with FP in general and in your specific question we probably need to see in order to help. – Jared Smith Dec 18 '20 at 13:04
  • 3
    I don't think you should (can) use `async` functions to combine tasks. – Bergi Dec 18 '20 at 13:19
  • 3
    Please be more specific what the code is supposed to do here. All those variable names ending in `monad` are quite meaningless - and probably wrong: types are monads, values are not. – Bergi Dec 18 '20 at 13:20
  • As I have already said I need make an async api request with value from `context.token` and modify context with data getted from this request. I don't think I be able more specific. `resultMonad` and `okMonad` both are monads (Result and Result.Ok). Only `context` is plain object. – HAGer Dec 20 '20 at 12:53
  • `Also how are you getting Maybe(Promise) out of an async function?` I don't think that I understood the question. – HAGer Dec 20 '20 at 12:59
  • So =) let's try again? – HAGer Dec 29 '20 at 08:50

0 Answers0