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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -