Js承诺等待,直到决心继续"主要"

0

的问题

我怎么能力的一种功能为解决未决承诺使用它之前在JavaScript

  generateKey(pass, iter).then(function(result) {
    Pkey = result;
  });

上述Pkey返回 <empty string> 之前解决的我可以写一个循环检查非的空虚,但那似乎有悖常理的,不是吗?

  Pkey = generateKey(pass, iter);

返回的一个未决的承诺,是最终履行了

function generateKey(passwd, iterations) {

  var encoder = new TextEncoder('utf-8');
  var passphraseKey = encoder.encode(passwd);
  var saltBuffer = encoder.encode("carthage");

  return crypto.subtle.importKey(
    'raw',
    passphraseKey,
    {name: 'PBKDF2'},
    false,
    ['deriveBits', 'deriveKey']
  ).then(function(key) {
//    console.log(key);
    return window.crypto.subtle.deriveKey(
    { "name": 'PBKDF2',
      "salt": saltBuffer,
      "iterations": iterations,
      "hash": 'SHA-256'
    },
    key,
    { "name": 'AES-CBC',
      "length": 256
    },
    true,
    [ "encrypt", "decrypt" ]
  )
  }).then(function (webKey) {
//    console.log(webKey);
    return crypto.subtle.exportKey("raw", webKey);
  }).then(function (buffer) {
//    console.log(buffer);
//    console.log(saltBuffer);
//    console.log("Private Key = " + buf2hex(buffer));
//    console.log("Salt = " + bytesToHexString(saltBuffer));
    return buffer;
  });

}



function buf2hex(buffer) { // buffer is an ArrayBuffer
  return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}



function bytesToHexString(byteArray) {
  return Array.prototype.map.call(byteArray, function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('');
}

asynchronous javascript promise
2021-11-23 04:58:14
1

最好的答案

1

你可以使用 异步 & 等待

async function generateKey(passwd, iterations) {...}
...
Pkey = await generateKey(pass, iter);
2021-11-23 05:13:48

但是,记住,这一功能将仍然返回的承诺,并呼叫者的这种职能仍然会有用 .then()await 得到解决价值的承诺,这一功能返回。
jfriend00

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................