javascript - Async/await not working -
i'm trying number 1 console.log first it's not. doing wrong?
thank you.
let timer = function(time, message) { settimeout((time) => { console.log(`alert: ${message}`); }, time); //return time; } const asyncdemo = async function asyncdemo(time, message) { try { var time1 = await timer(3000, "number 1"); } catch (e) { console.log("critical error!"); } try { var time2 = await timer(1000, "number 2"); } catch (e) { console.log("critical error!"); } } asyncdemo();
ansync/await depends on promises work. function needs return promise. instance:
let timer = function(time, message) { return new promise((resolve, reject) =>{ settimeout((time) => { console.log(`alert: ${message}`); resolve() }, time); }) }
Comments
Post a Comment