Reputation: 29
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
Upvotes: 2
Views: 1784
Reputation: 1737
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
Upvotes: 1