-1

How do I use an asynchronous IIFE inside the callback as per this example here to avoid the error message: Promise returned in function argument where a void return was expected.

  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!');
      }
    );
  }
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • If you can write an async IIFE, why not just make signIn async? Then it's easier to read and you can guarantee a void promise with a bare `return;`. – jonrsharpe May 04 '21 at 22:37
  • 1
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi May 04 '21 at 22:45
  • What tool is giving that error message? On what line/function specifically? – Bergi May 04 '21 at 22:46
  • No, you should not use an IIFE. And are you saying that you want to use `async`/`await`? – Bergi May 04 '21 at 22:47
  • @Bergi the error message happens at `return new Promise((resolve, reject) =>`. I'm not sure what I should use. I'd just like to figure out a way to get rid of the ESLint error message. – methuselah May 04 '21 at 22:53

1 Answers1

1

The new Promise executor callback should return void, but you're passing an arrow function with a concise body that implicitly returns the value. You could easily fix that by doing

return new Promise((resolve, reject) => {
//                                      ^
   … /*
})
^ */

but really you just shouldn't be using the Promise constructor at all! Simply

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!');
  });
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375