1.回调使用promise,同步执行
1 let pro = function(data){ 2 return new Promise((resolve,reject)=>{//使函数return对象,使能then 3 setTimeout(()=>{ 4 console.log(data); 5 resolve("hello!"); 6 },1000) 7 }) 8 }; 9 10 pro(1).then(function(res){//res:接收成功 "hello!", then(()=>pro(2)) 不写{},自带return 11 console.log(res); 12 return pro(2); 13 }).then(function(res){ 14 console.log(res); 15 pro(3)}); 16 17 结果: 18 1 19 hello! 20 2 21 hello! 22 3