javascript - 承诺不交回报
问题描述
class UserStorage {
async loginUser(id, pw) {
await setTimeout(() => {
if (
(id === "ellie" && pw === "dream") ||
(id === "coder" && pw === "academy")
)
return id;
else return new Error("not found user info");
}, 1000);
}
async getRoles(user) {
if (user === "ellie") return { name: "ellie", role: "admin" };
else return new Error("no access");
}
}
const userStorage = new UserStorage();
const id = prompt("wowSans");
const password = prompt("pw");
userStorage
.loginUser(id,password)
.then(userStorage.getRoles)
.then((value) => console.log(`Hi ${value.name}! Your Role is ${value.role}!`))
.catch(console.log);
我要打印“嗨,艾莉!您的角色是管理员!” 但它不起作用。它打印“嗨,错误!您的角色未定义!” 我今天第一次遇到 Promise 和 async/await。有人知道合适的解决方案吗?请回答!
解决方案
推荐阅读
- dictionary - 在字典中搜索和替换部分匹配
- javascript - NextJS Firebase 身份验证 InitialiseApp 错误
- firebase - DatabaseException:无法获取 FirebaseDatabase 实例:在 FirebaseApp 中或从您的 GetInstance() 调用中指定 DatabaseURL
- azure - Databricks Notebook 作为 livy 会话端点的替代品
- email - 为什么正则表达式不适用于输入类型“电子邮件”?
- typescript - 如何将带有警报注释的图形小部件添加到 Cloudwatch 仪表板
- javascript - discord.js 上的 ExtendedMessage 是什么
- mysql - MYSQL:如何从一列到多列中选择多个值?
- api - 为什么会发布请求连接但获取不会?
- php - 我如何使用 curl 获取此网址