在回调中使用异步 IIFE
Use an asynchronous IIFE inside the callback
如何按照此示例在回调中使用异步 IIFE 以避免错误消息:Promise returned in function argument where a void return 预计 .
signIn(email: string, password: string, course?: ICourse): Promise<void> {
return new Promise<UserCredential>((resolve, reject) =>
this.afAuth.signInWithEmailAndPassword(email, password).then(
(res) => {
resolve(res);
},
(error: { message: string }) => {
reject(error);
this.toastrService.warning('Something has gone wrong. Please try again.', 'Oops!');
this.logger.debug('An error occurred during Email Sign In');
this.logger.error(error.message);
}
)
).then(
(result: UserCredential) => {
if (course && result.user) {
this.builderSignIn(course, result.user.uid);
} else {
if (result != null) {
this.ngZone.run(() => {
void this.router.navigate(['dashboard']);
});
}
}
},
(error: { message: string }) => {
this.toastrService.warning(error.message, 'Oops!');
}
);
}
new Promise
执行器回调应该 return void
,但是您传递的箭头函数具有隐式 return 值的简洁主体。您可以通过
轻松解决这个问题
return new Promise((resolve, reject) => {
// ^
… /*
})
^ */
但实际上你只是 shouldn't be using the Promise
constructor!简直
signIn(email: string, password: string, course?: ICourse): Promise<void> {
return this.afAuth.signInWithEmailAndPassword(email, password).then((result: UserCredential) => {
if (course && result.user) {
this.builderSignIn(course, result.user.uid);
} else {
if (result != null) {
this.ngZone.run(() => {
void this.router.navigate(['dashboard']);
});
}
}
}, (error: { message: string }) => {
this.toastrService.warning('Something has gone wrong. Please try again.', 'Oops!');
this.logger.debug('An error occurred during Email Sign In');
this.logger.error(error.message);
this.toastrService.warning(error.message, 'Oops!');
});
}
如何按照此示例在回调中使用异步 IIFE
signIn(email: string, password: string, course?: ICourse): Promise<void> {
return new Promise<UserCredential>((resolve, reject) =>
this.afAuth.signInWithEmailAndPassword(email, password).then(
(res) => {
resolve(res);
},
(error: { message: string }) => {
reject(error);
this.toastrService.warning('Something has gone wrong. Please try again.', 'Oops!');
this.logger.debug('An error occurred during Email Sign In');
this.logger.error(error.message);
}
)
).then(
(result: UserCredential) => {
if (course && result.user) {
this.builderSignIn(course, result.user.uid);
} else {
if (result != null) {
this.ngZone.run(() => {
void this.router.navigate(['dashboard']);
});
}
}
},
(error: { message: string }) => {
this.toastrService.warning(error.message, 'Oops!');
}
);
}
new Promise
执行器回调应该 return void
,但是您传递的箭头函数具有隐式 return 值的简洁主体。您可以通过
return new Promise((resolve, reject) => {
// ^
… /*
})
^ */
但实际上你只是 shouldn't be using the Promise
constructor!简直
signIn(email: string, password: string, course?: ICourse): Promise<void> {
return this.afAuth.signInWithEmailAndPassword(email, password).then((result: UserCredential) => {
if (course && result.user) {
this.builderSignIn(course, result.user.uid);
} else {
if (result != null) {
this.ngZone.run(() => {
void this.router.navigate(['dashboard']);
});
}
}
}, (error: { message: string }) => {
this.toastrService.warning('Something has gone wrong. Please try again.', 'Oops!');
this.logger.debug('An error occurred during Email Sign In');
this.logger.error(error.message);
this.toastrService.warning(error.message, 'Oops!');
});
}