【发布时间】:2020-08-27 03:17:51
【问题描述】:
我有一个场景,其中有一个功能可以检查是否需要 userWhiteListing。如果没有,则调用 allowUserToLogin 函数。如果是,则检查特定用户是否被列入白名单。如果未列入白名单则抛出错误,否则调用 allowUserToLogin 函数。
我正在寻找一种更好的 promise 方法,而不是 async await。
这是一个相同的示例。
public function sample():Promise<{ session: string , url: string }> {
return this.userWhiteListingRqd().then((whiteListingRqd) => {
if(whiteListingRqd) {
return this.isUserWhiteListed().then((userWhiteListed) => {
if(!userWhiteListed) {
throw new Error("error");
}
// Here if user whitelisted then it will go to allowUserToLogin
// But here typescript throws error Promise<void> cannot be assignable
// to Promise<session: string , url:string>
});
}
});
return this.allowUserToLogin().then(() => {
return { session, url };
});
}
private function userWhiteListingRqd(): Promise<boolean> {
// returns boolean value
}
private isUserWhiteListed(): Promise<boolean> {
// returns boolean value
}
我仍处于学习阶段。有没有更好的方法来使用 Promise 处理这种情况。
任何帮助将不胜感激。
【问题讨论】:
标签: javascript node.js typescript promise