【发布时间】:2019-06-13 13:39:16
【问题描述】:
我正在学习如何将 class 与 Async/Await 一起使用。我想我在 Run 类中的 getData 函数做错了。
使用await get()(实验)时应该输出“Hello World”。
当我运行脚本时,我得到一个错误:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这 错误源于在异步函数内部抛出 没有 catch 块,或拒绝未处理的承诺 使用 .catch()。 (拒绝编号:1)
类脚本:
class Run {
constructor() {
this.stop = false;
}
async getData(entry) {
if (this.stop) {
console.log("Stopped")
return;
}
return await this.get();
}
async get() {
return "Hello World";
}
stop() {
this.stop = true;
}
}
用法:
let run = new Run();
run.getData(async (entry) => {
console.log(entry);
});
【问题讨论】:
-
你从不在任何地方使用
entry? -
方法和数据属性不能同时命名为
stop。
标签: javascript node.js async-await