【发布时间】:2019-04-02 23:07:26
【问题描述】:
我怎样才能使这个调用阻塞(例如,使用 async/await)?
testMethod(message) {
let signature;
eccrypto.sign(this.privateKey, msg)
.then(function (sig) {
console.log("Signature in DER format:", sig);
signature = sig;
});
return signature;
}
我希望 testMethod 返回signature,现在返回(当然)undefined!我一直在玩async/await,但没有成功...
有什么帮助吗?
【问题讨论】:
-
async/await 只是用更好的语法处理 Promise 的方法。他们不会阻止异步代码异步。
-
一件小事,但您在函数定义中使用了
message,但在对eccrypto.sign的调用中使用了msg。这是故意的吗?这让我想到另一点,你应该始终处理你的错误,如果使用老式的 promise 语法,则使用.catch(),或者如果使用 async/await 则使用 try/catch
标签: javascript node.js async-await