83251242 2019-06-21
//用来解决异步的一些问题 let p = new Promise(function(resolve,refused){ console.log("abc"); //resolve(); setTimeout(function(){ resolve(); },3000); console.log("123"); }); p.then(function(){ console.log("def"); }); //挂起状态 成功状态 失败状态 setTimeout(function(){ console.log(1); setTimeout(function(){ console.log(2); setTimeout(function(){ console.log(3); setTimeout(function(){ console.log(4); setTimeout(function(){ console.log(6); },5000); },3000); },3000); },2000); },1000); function sleep(times){ return new Promise(function(resolve,refused){ setTimeout(function(){ resolve(); },times); }); } let pr = new Promise(function(resolve,refused){ setTimeout(function(){ resolve(); },1000); }); pr.then(function(){ console.log(1); return sleep(2000); }).then(function(){ console.log(2); return sleep(3000); }).then(function(){ console.log(3); return sleep(4000); }).then(function(){ console.log(4); }); let p3 = new Promise(function(resolve,refused){ resolve(); refused("失败了"); }); p3.then(function(){ console.log("edf"); }).catch(function(e){ console.log(e); });