Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JS await execution and then placement without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I have the following code,
await getAuthFailedAttempts().then((attempt) => { errorMessage = VALIDATION_ERROR; if (!attempt.canRetry) { errorMessage = createFailedAttemptsError(attempt); } else { setAuthFailedAttempt(); } this.setState({ loading: false, passcode: "", validationError: errorMessage, });
So how to rewrite to use await properly,
const attempt = await getAuthFailedAttempts();
and how to place the “then” for “attempt”?
thanks in advance
Answer
You’re pretty much there– once you have assigned the result of getAuthFailedAttempts()
to attempt
using await
then what was the body of your .then
simply comes behind it:
const attempt = await getAuthFailedAttempts(); errorMessage = VALIDATION_ERROR; if (!attempt.canRetry) { errorMessage = createFailedAttemptsError(attempt); } else { setAuthFailedAttempt(); } this.setState({ loading: false, passcode: "", validationError: errorMessage, });
Please note that to use await
the containing function
must be an async function
declaration.
We are here to answer your question about JS await execution and then placement - If you find the proper solution, please don't forgot to share this with your team members.