await/async 是 ES7 最重要特性之一,它是目前爲止 JS 最佳的異步解決方案了。雖然沒有在 ES2016 中錄入,但很快就到來,目前已經在 ES-Next Stage 4 階段。javascript
直接上例子,好比咱們須要按順序獲取:產品數據=>用戶數據=>評論數據java
傳統的寫法,無需解釋git
// 獲取產品數據 ajax('products.json', (products) => { console.log('AJAX/products >>>', JSON.parse(products)); // 獲取用戶數據 ajax('users.json', (users) => { console.log('AJAX/users >>>', JSON.parse(users)); // 獲取評論數據 ajax('products.json', (comments) => { console.log('AJAX/comments >>>', JSON.parse(comments)); }); }); });
Promise 已經被說起已久了,也是 ES6 的一部分。Promise 能消除 callback hell 帶來的厄運金字塔,相比起來代碼更清晰了,但仍是達不到編寫同步代碼的直觀程度。github
// Promise // 封裝 Ajax,返回一個 Promise function requestP(url) { return new Promise(function(resolve, reject) { ajax(url, (response) => { resolve(JSON.parse(response)); }); }); } // 獲取產品數據 requestP('products.json').then((products) => { console.log('Promises/products >>>', products); // 獲取用戶數據 return requestP('users.json'); }).then((users) => { console.log('Promises/users >>>', users); // 獲取評論數據 return requestP('comments.json'); }).then((comments) => { console.log('Promises/comments >>>', comments); });
Generators 也是 ES6 一個新的特性,可以 暫停/執行 代碼。yield 表示暫停,iterator.next 表示執行下一步,若是你不瞭解 Generators 也不要緊,能夠忽略它直接學習 await/async。ajax
// Generators function request(url) { ajax(url, (response) => { iterator.next(JSON.parse(response)); }); } function *main() { // 獲取產品數據 let data = yield request('products.json'); // 獲取用戶數據 let users = yield request('users.json'); // 獲取評論數據 let products = yield request('comments.json'); console.log('Generator/products >>>', products); console.log('Generator/users >>>', users); console.log('Generator/comments >>>', comments); } var iterator = main(); iterator.next();
與 Promise 結合使用json
// 封裝 Ajax,返回一個 Promise function requestP(url) { return new Promise(function(resolve, reject) { ajax(url, (response) => { resolve(JSON.parse(response)); }); }); } (async () => { // 獲取產品數據 let data = await requestP('products.json'); // 獲取用戶數據 let users = await requestP('users.json'); // 獲取評論數據 let products = await requestP('comments.json'); console.log('ES7 Async/products >>>', products); console.log('ES7 Async/users >>>', users); console.log('ES7 Async/comments >>>', comments); }());
與 Fetch API 結合使用:異步
(async () => { // Async/await using the fetch API try { // 獲取產品數據 let products = await fetch('products.json'); // Parsing products let parsedProducts = await products.json(); // 獲取用戶數據 let users = await fetch('users.json'); // Parsing users let parsedUsers = await users.json(); // 獲取評論數據 let comments = await fetch('comments.json'); // Parsing comments let parsedComments = await comments.json(); console.log('ES7 Async+fetch/products >>>', parsedProducts); console.log('ES7 Async+fetch/users >>>', parsedUsers); console.log('ES7 Async+fetch/comments >>>', parsedComments); } catch (error) { console.log(error); } }());
再次結合 Fetchasync
(async () => { let parallelDataFetch = await* [ (await fetch('products.json')).json(), (await fetch('users.json')).json(), (await fetch('comments.json')).json() ]; console.log('Async parallel+fetch >>>', parallelDataFetch); }());
使用 await/async 用同步的思惟去解決異步的代碼,感受很是酷很是爽!學習
參考文獻[原文]:https://github.com/jaydson/es...fetch