我正在尝试使用 await 为异步方法获取有效的 jwt token 。 如何解决await isValid in function?
module.exports.resolveAfter2Seconds = async function resolveAfter2Seconds(jwt) {
const cert = fs.readFileSync(__dirname + ‘/...’); // get public key
let a = await isValid(jwt, cert)
};
async function isValid(jwt, cert) {
return jwtWebToken.verify(jwt, cert, function (err, decoded) {
console.log(decoded);
if (! decoded) {
throw new Error(“error”);
}
return decoded;
})
}
最佳答案
尝试下面的代码。
function isValid(jwt, cert) {
return new Promise(function(resolve) {
jwtWebToken.verify(jwt, cert, function (err, decoded) {
if (! decoded) {
throw new Error(“error”);
}
resolve(decoded)
})
});
}
关于javascript - 在函数中解决 await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56393231/