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

bonus es8 async await
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Bonus: ES8 Async & Await

Shan-Hung Wu & DataLab CS, NTHU

slide-2
SLIDE 2

Outline

  • ES6 Promises
  • ES8 Async & Await
  • Pitfalls

2

slide-3
SLIDE 3

ES6 Promise

  • A value available

in the future

  • Separation of

concerns

– Handlers can be written in different places

  • Use arrow func

for this

3

// in method1() const p = new Promise((resolve, reject) => { ... // do asynchronous job here if (success) resolve(data); else reject(err); }); return p; // in method2(p) const p2 = p.then(data => { ... // process data return data2 }); // always returns a new Promise return p2; // in method3(p2) p2.then(data2 => { ... process data2 }).catch(err => { ... // handle err }); // always returns a new Promise

slide-4
SLIDE 4

Execution Flow

  • Chain then and/or

catch as long as you like

  • Reject mode:

– throw new Error()

  • Resolve mode:

– return

4

resolve() reject() return throw return throw

slide-5
SLIDE 5

Axios and AJAX Requests

  • Requests can be canceled

5

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(...);

slide-6
SLIDE 6

Outline

  • ES6 Promises
  • ES8 Async & Await
  • Pitfalls

6

slide-7
SLIDE 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

slide-8
SLIDE 8

Example

8

// ES6 Promise function getFirstUser() { return getUsers().then(users => users[0].name) .catch(err => ({ name: 'default user' })); } // ES7 Async/Await async function getFirstUser() { try { // line blocked until promise // resolved/rejected. let users = await getUsers(); return users[0].name; } catch (err) { return { name: 'default user' }; } }

  • An async function

returns a promise

  • Await on a promise

until value available

  • Try/catch for

resolve/reject

slide-9
SLIDE 9

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' }; } }

AJAX, the Async/Await Style

9

slide-10
SLIDE 10

Outline

  • ES6 Promises
  • ES8 Async & Await
  • Pitfalls

10

slide-11
SLIDE 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

slide-12
SLIDE 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

12

await axios.get('...url...’); // OK function getUser(id) { return await axios.get('...url...’); // error } async function getUser(id) { return await axios.get('...url...’); // OK }

slide-13
SLIDE 13

const axios = require('axios’); async function getUsers(ids) { let users = []; try { ids.forEach(async id => { let user = await axios.get('...url...’); users.push(user); }); } catch (err) {...} return users; } const vips = await getUsers(...);

Pitfall #3: Array.forEach()

  • Empty users

returned immediately

– Elements added later

13

slide-14
SLIDE 14

Solution: Use for Instead

14

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(...);

slide-15
SLIDE 15

Pitfall #4: Reduced Parallelism

  • If order doesn’t

matter, why get user sequentially?

15

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(...);

slide-16
SLIDE 16

16

// 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

Parallel Awaiting

slide-17
SLIDE 17

Solution

17

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(...);