Skip to content
Advertisement

Function does not return true even if it goes through

I can’t seem to make this work but I want to return true every-time the function executes successfully, which in this case is “Changing the password”.

JavaScript

The res variable when called by other functions always results in false even though I see the toast message “Password changed successfully”.

JavaScript

The if condition in above code never executes because res is always false. I know it has something to do with async await but can’t figure out how to make it work

Advertisement

Answer

JavaScript

Has many logical errors in it.

First you should decide whether you’re going to use async and its feature await or classical Promise .thenable -style. Please do not use both, it will only confuse you and the reader.

Let’s ditch the async (since you don’t even use await), and Promises are chainable, you do not (and MUST not) nest .then blocks, use this instead:

JavaScript

The key here is that ChangePassword itself returns a Promise.*

The caller is then responsible to call it with .then or use async in combination with await:

JavaScript

The same code looks a lot cleaner if you use async:

JavaScript

(If you were wondering how that would look like).

*a function tagged as async ALWAYS returns a promise, by the way.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement