首页 > 解决方案 > 如何返回结果值而不是承诺?

问题描述

我试图从承诺中获得返回值,但我做不到。

我正在从“react-native-sha256”导入 { sha256 }

const sha256 = sha256(keytohash);
return sha256;

承诺 {_40: 0, _65: 0, _55: null, _72: null} <<< 这是我在 console.log 时得到的

我也试过:

const sha256 = async () => {
const key = await sha256(keytohash).then(hash => (hash));
return key;
};

return sha256;

承诺 {_40: 0, _65: 0, _55: null, _72: null} <<< 这是我在 console.log 时得到的

反正有没有让我得到哈希值而不是承诺对象?

标签: react-nativepromise

解决方案


尝试!

const getSha256 = async () => {
  const key = await sha256(keytohash)
  return key;
}

console.log(getSha256())

您返回了异步函数而不是值。

Math.random = function
Math.random() = value

您需要使用 await 来等待 promise.resolve。


推荐阅读