2

     first() {
        setTimeout(() => console.log('I am First CALL afer 1 second'), 1000)
      }
      second() {
          console.log("SEcond methof is called");
      }
  
      async  getMoviesFromApi() {
        try {
          let response = await this.first() ;
    
          let response2 =  this.second() ;

        } catch (error) {
          console.error(error);
        }
      }

Here second function should be called after function first and first will print after 1 second but second is called directly , it is not waiting for function first. please help

1 Answer 1

1

You are not returning a Promise, you are just calling a setTimeout that's not an awaitable

You should do something like this:

first() {
   return new Promise(function (resolve, reject) {
        setTimeout(() => {
              console.log('I amFirest CAll afer 1 second');
              resolve();
        }, 1000)
   });
}

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.