gpt4 book ai didi

javascript - 在 es6 中,我需要 promise 在完成后返回 true 或 false

转载 作者:行者123 更新时间:2023-12-03 05:43:08 24 4
gpt4 key购买 nike

在 es6 中,我需要 promise 在完成时返回 true 或 false

这是我的尝试,但它返回一个 promise ,我需要它是真还是假

return this.isUserAuthenticated()
.then(() => this.checkIfTeacherProfileExists());

返回部分需要为 true 或 false。

这是其中一个返回的样子:

  isUserAuthenticated = (): Promise<any> => {
return new Promise((resolve, reject) => {
this.authService.checkIfLoggedIn().then((isAuthUser: boolean) => {
resolve(true);
});
});
}

最佳答案

您的返回是一个持有 truefalse 的 promise 。因此,您所要做的就是链接另一个将保存 bool 值的 .then()

return this.isUserAuthenticated()
.then(() => this.checkIfTeacherProfileExists())
.then(v => console.log(v)); // true or false

关于您的评论,请参阅@FelixKling 的评论 - 您不能这样做,这不是 Promise 的工作原理。

关于javascript - 在 es6 中,我需要 promise 在完成后返回 true 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40440240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com