Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Async/await与SubTecryPto结合使用_Javascript_Promise_Async Await_Redux - Fatal编程技术网

Javascript Async/await与SubTecryPto结合使用

Javascript Async/await与SubTecryPto结合使用,javascript,promise,async-await,redux,Javascript,Promise,Async Await,Redux,我在MDN上注意到以下内容,这使我相信有一种方法可以将sublecrypto函数的结果分配给变量。但据我所知/已经研究过async/await,只能在async函数中使用await async function sha256(message) { const msgBuffer = new TextEncoder('utf-8').encode(message); // encode as UTF-8 const hashBuffer =

我在MDN上注意到以下内容,这使我相信有一种方法可以将sublecrypto函数的结果分配给变量。但据我所知/已经研究过async/await,只能在
async
函数中使用
await

async function sha256(message) {
    const msgBuffer = new TextEncoder('utf-8').encode(message);                     // encode as UTF-8
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);            // hash the message
    const hashArray = Array.from(new Uint8Array(hashBuffer));                       // convert ArrayBuffer to Array
    const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join(''); // convert bytes to hex string
    return hashHex;
}

sha256('abc').then(hash => console.log(hash));

const hash = await sha256('abc');
这个例子是不正确的还是我误解了什么?最重要的是,;是否可以在不使用
的情况下将sublecrypto/Promise的结果分配给变量。then()

那些自问为什么我需要/想要这个的人。我将WebCrypto与redux persist结合使用,但它似乎无法处理基于承诺的问题。

该示例具有误导性(或不完整),您确实不能在
异步函数
之外使用
等待
。我刚刚编辑了它(MDN是一个维基!)

是否可以在不使用
的情况下将sublecrypto/Promise的结果分配给变量。then()


是的,它将promise对象存储在变量中。要访问promises结果,您需要使用
然后
等待

好吧,我想我快疯了。。。谢谢你的澄清。看来到时候我得潜入redux了。