javascript - How to print the message using setTimeout function with the help of callback and promise concept? -
below code snippet have written.my aim print message.
delayedalert(message:string, time: number, cb){ settimeout(()=>{ cb() }, time) };
//calling function.
delayedalert('aditya', 3000, ()=>{ console.log('done) });
i want print aditya after 3secs, getting console value, don't want instead want aditya printed after 3 secs. , same above i have write code promise also. please ignore typos.
please help.
the promise won't need call back, act placeholder result of asynchronous task.... in example can execute function when promise resolved .. (in .then() funcion)
function delayedalert(message, time){ return new promise((resolve,reject)=>{ settimeout(()=>resolve(message), time); }); }; //calling function. //promise let promise = delayedalert('aditya', 3000); promise.then(message=>{ console.log(message); }); //callback function delayedalertcallback(message, time,cb){ settimeout(()=>cb(message), time); }; delayedalertcallback('aditya2',4000,message=>console.log(message));
Comments
Post a Comment