When we can put a setTimeout around the callback call within any function definition, or, wrap around the callback call inside a setTimeout, thereby restricting the return of that function to our specified time, what is the advantage in promises, then?
///////// in case of asynchronous callbacks method//////
function a(v,cb){
setTimeout(function() {
console.log("Hello")
},5000)
cb(v);
}
a(1,function(v){
setTimeout(function(){
console.log(v);
},10000)
})
////////// in case of asynchronous promise method/////////////
function a(v){
return new Promise(function(resolve, reject){
var v=1;
if(v===1){
setTimeout(function(){
console.log(“Hello”)
console.log()
resolve(v);
}, 5000)
}else reject(new Error(“Sorry error”))
})
}
var v=1;
let q = a(v);
setTimeout(function(){
q.then(function(){
console.log(v);
}).catch(function(err){
console.error(err)
})
},10000)