这是我的代码
encrypt(plaintxt){
const secretKey = '12456780123456';
const iv = "124567890123456";
let a = "awal";
AesCrypto.encrypt(plaintxt,secretKey,iv).then(cipher=>{
a = cipher;
}).catch(err=>{
a = err;
});
return a;
}
如何在 AesCrypto.encrypt 函数中设置变量 a 的值?谢谢。
AesCrypto.encrypt()
是异步的,这意味着如果你想从你的 encrypt()
返回 a
的值使用您在上面定义的结构函数,那么您需要像这样 define it as an asynchronous function:
/* Declare the function as asynchronous with async keyword */
function async encrypt(plaintxt){
const secretKey = '124567980123456';
const iv = "1234567890123456";
/* Create a promoise and wait for it to complete (or fail)
using the await keyword */
const a = await (new Promise((resolve, reject) => {
/* Resolve or reject the promise by passing the handlers
to your promise handlers */
AesCrypto.encrypt(plaintxt,secretKey,iv)
.then(resolve)
.catch(reject);
}))
return a;
}