Js slib, počkejte, až se rozhodl pokračovat v "hlavní"

0

Otázka

Jak mohu vynutit funkce vyřešit čekající slib před použitím v Javascriptu

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

výše Pkey vrátí <empty string> před řešení bych mohl napsat smyčku pro kontrolu, pro non-prázdnota, ale to se zdá neintuitivní, ne?

a

  Pkey = generateKey(pass, iter);

vrátí čekající slíbit, že se nakonec splnil

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

Nejlepší odpověď

1

Můžete použít asynchronní a čekají , jak je uvedeno níže

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

Ale, mějte na paměti, že tato funkce bude JEŠTĚ vrátit slib a volající této funkce bude ještě muset použít .then() nebo await získat vyřešen hodnotu z slíbit, že tato funkce vrací.
jfriend00

V jiných jazycích

Tato stránka je v jiných jazycích

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