1
  React.useEffect(_ => {
    ReactNativeAsyncStorage.getItem("jwt")
    |> Js.Promise.then_(jwt => {Js.log(jwt)});
    None;
  });

Error:

This expression has type unit but an expression was expected of type
         Js.Promise.t('a) = Js.Promise.t('a)

I am using https://github.com/reason-react-native/async-storage with https://github.com/react-native-community/async-storage

glennsl
  • 28,186
  • 12
  • 57
  • 75

1 Answers1

2

The type of Js.Promise.then_ is

('a => t('b), t('a)) => t('b)

which means the function passed to it must return a, and that then_ itself will return that promise.

You're making two mistakes in your code:

  1. You are not returning a promise from the function you pass to then_
  2. You are not handling the promise returned by then_.

The following fixes both:

  React.useEffect(_ => {
    let _: Js.Promise.t(unit) =
      Js.Prmoise.(
        ReactNativeAsyncStorage.getItem("jwt")
        |> then_(jwt => resolve(Js.log(jwt)))
      );
    None;
  });

let _: Js.Promise.t(unit) = ... uses the wildcard pattern to discard the result of the following expression. The type of the result is Js.Result.t(unit), which is annotated to protect against accidental partial application.

resolve(Js.log(jwt)) will return a promise with the result of calling Js.log, which is unit, hence why the resulting promise has the type Js.Promise.t(unit).

See the Reason documentation for more on how to use promises.

glennsl
  • 28,186
  • 12
  • 57
  • 75
  • Why is getItem wrapped in a promise again. – a-c-sreedhar-reddy Mar 29 '20 at 05:07
  • 2
    It isn't. `Js.Promise.(...)` just opens the namespace so you don't have to repeatedly qualify its functions. It's the same as `open Js.Promise`, except constrained to a single expression. See https://reasonml.github.io/docs/en/module#open-ing-a-module – glennsl Mar 29 '20 at 08:18