使用React Native异步存储

问题描述 投票:0回答:1
  React.useEffect(_ => {
    ReactNativeAsyncStorage.getItem("jwt")
    |> Js.Promise.then_(jwt => {Js.log(jwt)});
    None;
  });

错误:

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

我将https://github.com/reason-react-native/async-storagehttps://github.com/react-native-community/async-storage一起使用

react-native promise asyncstorage reason
1个回答
0
投票

Js.Promise.then_的类型是

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

这意味着传递给它的函数必须返回a,并且then_本身将返回该承诺。

您在代码中犯了两个错误:

  1. 您没有从传递给then_的函数中返回承诺”>
  2. 您未处理then_返回的承诺。
  3. 以下两项均修复:

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

let _: Js.Promise.t(unit) = ...使用通配符模式来丢弃以下表达式的结果。结果的类型为Js.Result.t(unit),带注释以防止意外的部分应用。

[resolve(Js.log(jwt))将返回一个承诺,并调用Js.log,即unit,因此得到的承诺的类型为Js.Promise.t(unit)

© www.soinside.com 2019 - 2024. All rights reserved.