回調(diào)與promise function f() { return new Promise(resolve => { setTimeout(function() { resolve(); }, 1000); }) } f() .then(function() { console.log(1); //return promise實(shí)例,,才能繼續(xù).then() return f(); }) .then(function() { console.log(2); return f(); }) .then(function() { console.log(4); return f(); }) .then(function() { console.log(3); return f(); }) .then(function() { console.log(5); return f(); }) .then(function() { console.log(6); }); 案例: let promise=new Promise(resolve=>{ console.log('Promise'); resolve(); }); promise.then(function(){ console.log('resolved'); }); console.log('Hello!'); //結(jié)果:Promise Hello! resolved Promise小動(dòng)畫案例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> #el { width: 100px; background: green; transition: all 1s; color: white; line-height: 100px; text-align: center; font-size: 40px; } </style> </head> <body> <div id="el">哦</div> <button id="btn">開始</button> <script src="./main.js"></script> </body> </html> main.js function moveTo(el, x, y) { return new Promise(resolve => { el.style.transform = `translate(${x}px, ${y}px)`; setTimeout(function() { resolve(); }, 1000); }); } let el = document.querySelector('div'); document.querySelector('button').addEventListener('click', e => { moveTo(el, 100, 100) .then(function() { console.log('第一次移動(dòng)'); return moveTo(el, 200, 200); }) .then(function() { console.log('第二次移動(dòng)'); }) .then(function() { console.log('第二次移動(dòng)'); }); }); 錯(cuò)誤處理 function f(val) { return new Promise((resolve, reject) => { if (val) { resolve({ name: '小明' }); } else { reject('404'); } }); } f(true) .then((data) => { console.log(data) }, e => { console.log(e); }) catch f(true) .then(data => { console.log(data); return f(false); }) .then(() => { console.log('我永遠(yuǎn)不會(huì)被輸出'); }) .then(() => { }) .catch(e => { console.log(e); return f(false) ; }); finally f(true) .then(data => { console.log(data); return f(false); }) .catch(e => { console.log(e); return f(false); }) .finally(() => { console.log(100); }); promise三種狀態(tài)
Promise.all方法可以把多個(gè)promise實(shí)例 包裝成一個(gè)新的promise實(shí)例 function getData1() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第一條數(shù)據(jù)加載成功'); resolve('data1'); }, 1000); }); } function getData2() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第二條數(shù)據(jù)加載成功'); resolve('data2'); }, 1000); }); } function getData3() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第三條數(shù)據(jù)加載成功'); resolve('data3'); }, 1000); }); } function getData4() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第四條數(shù)據(jù)加載成功'); resolve('data4'); }, 2000); }); } // 所有數(shù)據(jù)都成功,則總決議成功,,并返回所有成功提示 let p = Promise.all([getData1(),getData2(),getData3(),getData4()]); p.then(arr => { console.log(arr); }); 一條數(shù)據(jù)失敗,,則總決議失敗,并返回錯(cuò)誤信息 function getData1() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第一條數(shù)據(jù)加載成功'); resolve('data1'); }, 1000); }); } function getData2() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第二條數(shù)據(jù)加載成功'); resolve('data2'); }, 1000); }); } function getData3() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第三條數(shù)據(jù)加載成功'); resolve('data3'); }, 1000); }); } function getData4() { return new Promise((resolve, reject) => { setTimeout(() => { reject('data4 err'); }, 500); }); } // 一條數(shù)據(jù)失敗,,則總決議失敗,,并返回錯(cuò)誤信息 let p = Promise.all([getData1(),getData2(),getData3(),getData4()]); p.then(arr => { console.log(arr); },e=>{ console.log(e); }); 空數(shù)組直接決議為成功 function getData1() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第一條數(shù)據(jù)加載成功'); resolve('data1'); }, 1000); }); } function getData2() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第二條數(shù)據(jù)加載成功'); resolve('data2'); }, 1000); }); } function getData3() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第三條數(shù)據(jù)加載成功'); resolve('data3'); }, 1000); }); } function getData4() { return new Promise((resolve, reject) => { setTimeout(() => { reject('data4 err'); }, 500); }); } // 空數(shù)組直接決議為成功 let p = Promise.all([]); p.then(() => { console.log('null'); },e=>{ console.log(e); }); promise.race function getData1() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第一條數(shù)據(jù)加載成功'); reject('err'); }, 500); }); } function getData2() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第二條數(shù)據(jù)加載成功'); resolve('data2'); }, 1000); }); } function getData3() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('第三條數(shù)據(jù)加載成功'); resolve('data3'); }, 1000); }); } let p = Promise.race([getData1(),getData2(),getData3()]); p.then(data => { console.log(data); }, e => { console.log(e); }) 空數(shù)組會(huì)被掛起 let p = Promise.race([]); Promise.resolve() 和 Promise.reject()
Promise.resolve傳遞一個(gè)普通的值 let p1 = new Promise(resolve => { resolve('成功!'); }); let p2 = Promise.resolve('成功!'); Promise.resolve傳遞一個(gè)promise實(shí)例 let poruomiesi = new Promise(resolve => { resolve('耶!') }); // 直接返回傳遞進(jìn)去的promise let p = Promise.resolve(poruomiesi); p.then(data => void console.log(data)); console.log(p === poruomiesi); Promise.resolve傳遞一個(gè)thenable let obj = { then(cb) { console.log('我被執(zhí)行了'); cb('哼,!'); }, oth() { console.log('我被拋棄了'); } } // 立即執(zhí)行then方法 Promise.resolve(obj).then(data => { console.log(data); }); Promise.reject Promise.reject({ then() { console.log(1) } }) .then(() => { console.log('我不會(huì)被執(zhí)行'); }, e => { console.log(e); }); 異步任務(wù)總是在同步任務(wù)之后執(zhí)行 function createAsyncTask(syncTask) { return Promise.resolve(syncTask).then(syncTask => syncTask()); } createAsyncTask(() => { console.log('我變成了異步任務(wù)?。?!'); return 1 + 1; }).then(res => { console.log(res); }); console.log('我是同步任務(wù)!'); 要求:多張圖片加載完之后才能進(jìn)行統(tǒng)一展示 const loadImg = src => { return new Promise((resolve, reject) => { const img = new Image(); img.src = src; img.onload=()=>{ resolve(img); }; img.onerror=(e)=>{ reject(e); }; // img.onload = void resolve(img); // img.onerror = void reject('加載失敗'); }); }; const imgs = [ 'http://img1.sycdn.imooc.com\/climg/5b16558d00011ed506000338.jpg', 'http://img1.sycdn.imooc.com\/climg/5b165603000146ca06000338.jpg', 'http://img1.sycdn.imooc.com//climg/5b1656140001c89906000338.jpg' ]; // map遍歷數(shù)組中的每一項(xiàng) Promise.all(imgs.map(src => loadImg(src))).then(arr => { console.log(arr); arr.forEach((img)=>{ document.body.appendChild(img); }); }); // map遍歷數(shù)組中的每一項(xiàng)(與上面相同作用,,上面是簡(jiǎn)寫) // map進(jìn)行循環(huán),每循環(huán)一次就將src作為參數(shù)傳遞進(jìn)來 // const promises=imgs.map(src =>{ // return loadImg(src); // }); // Promise.all(promises).then(arr => { // console.log(arr); // arr.forEach((img)=>{ // document.body.appendChild(img); // }); // }).catch((e)=>{ // console.log(e); // }); |
|