how to return result value instead of promise?

时间:2019-04-17 02:47:50

标签: react-native promise

I'm trying to get the return value from a promise but I couldn't.

I'm importing { sha256 } from 'react-native-sha256'

const sha256 = sha256(keytohash);
return sha256;

Promise {_40: 0, _65: 0, _55: null, _72: null} <<< this is what I get when I console.log

I also tried:

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

return sha256;

Promise {_40: 0, _65: 0, _55: null, _72: null} <<< this is what I get when I console.log

Is there anyway to let me get the hash value instead of the promise object?

2 个答案:

答案 0 :(得分:0)

尝试!

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

console.log(getSha256())

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

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

您需要等待promise。使用await解决。

答案 1 :(得分:0)

对不起,我无法尝试,但是我会像这样

async () => {
    await sha256(keytohash)
    .then(hash => JSON.parse(hash))
    .then(json => {
      console.log(JSON.stringify(json))
    })
    };