bonus es8 async await
play

Bonus: ES8 Async & Await Shan-Hung Wu & DataLab CS, NTHU - PowerPoint PPT Presentation

Bonus: ES8 Async & Await Shan-Hung Wu & DataLab CS, NTHU Outline ES6 Promises ES8 Async & Await Pitfalls 2 // in method1() const p = new Promise((resolve, reject) => { ... // do asynchronous job here ES6 Promise


  1. Bonus: ES8 Async & Await Shan-Hung Wu & DataLab CS, NTHU

  2. Outline • ES6 Promises • ES8 Async & Await • Pitfalls 2

  3. // in method1() const p = new Promise((resolve, reject) => { ... // do asynchronous job here ES6 Promise if (success) resolve(data); else reject(err); }); return p; • A value available // in method2(p) in the future const p2 = p. then (data => { ... // process data • Separation of return data2 concerns }); // always returns a new Promise return p2; – Handlers can be written in // in method3(p2) p2. then (data2 => { different places ... process data2 • Use arrow func }). catch (err => { ... // handle err for this }); // always returns a new Promise 3

  4. Execution Flow resolve() reject() • Chain then and/or catch as long as you like • Reject mode: – throw new Error() throw return • Resolve mode: – return return throw 4

  5. Axios and AJAX Requests const axios = require('axios’); // GET request axios.get('...url...').then(res => { res.status // HTTP response code (e.g., 200, 401) res.data // object parsed from HTTP response body res.headers // HTTP presonse headers }).catch(err => { console.log(err.response.status); }); // POST request axios.post('...url...', { ... // request body }).then(...).catch(...); • Requests can be canceled 5

  6. Outline • ES6 Promises • ES8 Async & Await • Pitfalls 6

  7. ES8 Async & Await • Goal: to make asynchronous code looks more consistent with synchronous code • Now, supported by major browsers and Node.JS v7.6+ 7

  8. Example // ES6 Promise function getFirstUser() { return getUsers(). then (users => users[0].name) . catch (err => ({ name: 'default user' })); } • An async function // ES7 Async/Await async function getFirstUser() { returns a promise try { // line blocked until promise • Await on a promise // resolved/rejected. let users = await getUsers(); until value available return users[0].name; } catch (err) { • Try / catch for return { name: 'default user' resolve/reject }; } } 8

  9. AJAX, the Async/Await Style const axios = require('axios’); async function getFirstUser() { try { let users = await axios.get('...url...'); return users[0].name; } catch (err) { console.log(err.response.status); return { name: 'default user' }; } } 9

  10. Outline • ES6 Promises • ES8 Async & Await • Pitfalls 10

  11. Pitfall #1: Mixing Callbacks/Promises • Choose one style, and stay consistent in your entire project – Callbacks – ES6 Promise – ES8 Async/await • Increases readability of your code 11

  12. Pitfall #2: await Not in async Function • You can use await at top level • But when using await is in a function, the function must be async await axios.get('...url...’); // OK function getUser(id) { return await axios.get('...url...’); // error } async function getUser(id) { return await axios.get('...url...’); // OK } 12

  13. Pitfall #3: Array.forEach() const axios = require('axios’); async function getUsers(ids) { let users = []; try { ids. forEach (async id => { let user = await axios.get('...url...’); users.push(user); }); • Empty users } catch (err) {...} returned immediately return users; } – Elements added later const vips = await getUsers(...); 13

  14. Solution: Use for Instead const axios = require('axios’); async function getUsers(ids) { let users = []; try { for (let id of ids) { let user = await axios.get('...url...’); users.push(user); } } catch (err) {...} return users; } const vips = await getUsers(...); 14

  15. Pitfall #4: Reduced Parallelism const axios = require('axios’); async function getUsers(ids) { let users = []; try { for (let id of ids) { let user = await axios.get('...url...’); users.push(user); } } catch (err) {...} • If order doesn’t return users; matter, why get } const vips = await getUsers(...); user sequentially? 15

  16. Parallel Awaiting // get a user object; blocked let fu = await getFirstUser(); // get a promise immediately; async jobs starts let fp = getFirstUser(); // sequential awaiting let fu = await getFirstUser(); let lu = await getLastUser(); // parallel awaiting let fp = getFirstUser(); // async jobs starts let lp = getLastUser(); // async jobs starts let [fu, lu] = await Promise.all ([fp, lp]); • Promise.all() creates a promise that resolves only when all child promises resolve 16

  17. Solution const axios = require('axios’); async function getUsers(ids) { let promises = []; try { for (let id of ids) { let promise = axios.get('...url...’); promises.push(promise); } } catch (err) {...} return await Promise.all (promises);; } const vips = await getUsers(...); 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend