recently learned ES6
, learned promise
, you can use then (). Then (). Then ()
to solve callback hell
I would like to ask:
like the following simple logic case, the two effects may be the same.
so what are the advantages of promise
?
if there is, where do you want to get the favor of the old driver, please do not hesitate to hit the gold code and hit two lines.
Thank you for your help
function afterSomeSeconds( callBackFn ){
setTimeout(()=>{
callBackFn("2");
}, 2 * 1000);
};
afterSomeSeconds(function toLog(msg){
console.log(msg)
});
console.log("1");
// ,
//
// ES6Promise
function afterSomeSecondsPromise(){
return new Promise(function(resolve, reject){
setTimeout(()=>{
resolve("3");
}, 3 * 1000);
})
}
afterSomeSecondsPromise().then(msg=>{
console.log(msg)
});
console.log("2");
:
1
2
2
3