Share
Embed
Music City Code: ES7 and Beyond!
This talks dives into some of the new (and experimental) features coming to ES7 and possibly later versions of JavaScript.
Jeremy Fairbank
August 29, 2015
Transcript
142 slides
-
Transpilers • Convert ES6/ES7/ES.later code into equivalent ES5 code. •
Babel • babeljs.io • Traceur • github.com/google/traceur-compiler -
TC39 Process • Five stage process for converting an idea
to a standard feature. • https://tc39.github.io/process-document -
Stage 0 - Strawman • Informal idea or proposal for
change or addition. • Function bind operator • Do expressions foo::bar(); let y = do { let x = 40; x + 2; } -
Stage 1 - Proposal • Make a case for addition
by describing use cases, challenges, high-level API. • Class and property decorators class Foo { @memoize bar() { return 42; } } -
Stage 2 - Draft • Precise syntax and semantics; initial
spec. • Object rest and spread properties • Async functions async function loadOrders() { const orders = await fetchJson('/orders'); console.log(orders); } let obj = { ...otherObj, x: 'foo' }; -
Stage 3 - Candidate • Completed spec; needs reviewing for
refinements. • Exponentiation operator • Array.prototype.includes 3 ** 2; // 9 [1, 2, 3].includes(2); // true -
Stage 4 - Finished • Ready to be included in
the next ES standard. • No proposals at Stage 4 at the moment. -
Function Bind Operator • Binary and unary double colon ::
operator • Bind this context to a function, by desugaring to bind, call, or apply. • https://github.com/zenparsing/es-function-bind • http://blog.jeremyfairbank.com/javascript/ javascript-es7-function-bind-syntax/ -
Call Syntax function logThis() { console.log(this); } const obj =
{ foo: 'bar' }; logThis.call(obj); // { foo: 'bar' } // Now obj::logThis(); // { foo: 'bar' } -
Call Syntax function logThis() { console.log(this); } const obj =
{ foo: 'bar' }; logThis.call(obj); // { foo: 'bar' } // Now obj::logThis(); // { foo: 'bar' } Normal invocations would log window or the global object. -
Call Syntax function logThis() { console.log(this); } const obj =
{ foo: 'bar' }; logThis.call(obj); // { foo: 'bar' } // Now obj::logThis(); // { foo: 'bar' } -
Call Syntax function logThis() { console.log(this); } const obj =
{ foo: 'bar' }; logThis.call(obj); // { foo: 'bar' } // Now obj::logThis(); // { foo: 'bar' } -
function logThis(...args) { console.log(this, ...args); } function logObj1(obj, ...args) {
logThis.apply(obj, args); } function logObj2(obj, ...args) { obj::logThis(...args); } const obj = { foo: 'bar' }; logObj1(obj, 42, 'baz'); // { foo: 'bar' } 42 'baz' logObj2(obj, 42, 'baz'); // { foo: 'bar' } 42 'baz' Apply Syntax -
Apply Syntax function logThis(...args) { console.log(this, ...args); } function logObj1(obj,
...args) { logThis.apply(obj, args); } function logObj2(obj, ...args) { obj::logThis(...args); } const obj = { foo: 'bar' }; logObj1(obj, 42, 'baz'); // { foo: 'bar' } 42 'baz' logObj2(obj, 42, 'baz'); // { foo: 'bar' } 42 'baz' -
Apply Syntax function logThis(...args) { console.log(this, ...args); } function logObj1(obj,
...args) { logThis.apply(obj, args); } function logObj2(obj, ...args) { obj::logThis(...args); } const obj = { foo: 'bar' }; logObj1(obj, 42, 'baz'); // { foo: 'bar' } 42 'baz' logObj2(obj, 42, 'baz'); // { foo: 'bar' } 42 'baz' -
Bind Syntax function logThis() { console.log(this); } const obj =
{ foo: 'bar' }; const logObj1 = logThis.bind(obj); logObj1(); // { foo: 'bar' } // Now const logObj2 = obj::logThis; logObj2(); // { foo: 'bar' } -
Bind Syntax function logThis() { console.log(this); } const obj =
{ foo: 'bar' }; const logObj1 = logThis.bind(obj); logObj1(); // { foo: 'bar' } // Now const logObj2 = obj::logThis; logObj2(); // { foo: 'bar' } -
Bind Syntax No parens, so bind instead of calling. function
logThis() { console.log(this); } const obj = { foo: 'bar' }; const logObj1 = logThis.bind(obj); logObj1(); // { foo: 'bar' } // Now const logObj2 = obj::logThis; logObj2(); // { foo: 'bar' } -
Explicit vs. Implicit Bind const obj = { foo: 'bar',
log() { console.log(this.foo); } }; const unbound = obj.log; const implicitBind = ::obj.log; const explicitBind = obj::obj.log; unbound(); // undefined implicitBind(); // bar explicitBind(); // bar -
const obj = { foo: 'bar', log() { console.log(this.foo); }
}; const unbound = obj.log; const implicitBind = ::obj.log; const explicitBind = obj::obj.log; unbound(); // undefined implicitBind(); // bar explicitBind(); // bar Explicit vs. Implicit Bind Lose this context -
Explicit vs. Implicit Bind const obj = { foo: 'bar',
log() { console.log(this.foo); } }; const unbound = obj.log; const implicitBind = ::obj.log; const explicitBind = obj::obj.log; unbound(); // undefined implicitBind(); // bar explicitBind(); // bar Implicitly bind to obj with unary operator. -
Explicit vs. Implicit Bind const obj = { foo: 'bar',
log() { console.log(this.foo); } }; const unbound = obj.log; const implicitBind = ::obj.log; const explicitBind = obj::obj.log; unbound(); // undefined implicitBind(); // bar explicitBind(); // bar Explicitly bind to obj with binary operator. -
Why Function Bind Operator? • Succinct syntax for call, apply,
and bind. • Virtual methods • Use array methods on DOM node lists. • Object method chaining with real methods and third party libs like Trine. • https://github.com/jussi-kalliokoski/trine • Callbacks to other libraries. -
Do Expressions • Make a block an expression. • Last
statement in the block is the “return” value. • Prepend do keyword to a block to create. • Like syntactical sugar for IIFE. • http://wiki.ecmascript.org/doku.php?id=strawman:do_expressions -
Do Expressions const foo = do { const x =
'b'; const y = 'a'; const z = 'r'; x + y + z; }; console.log(foo); // bar console.log(do { const x = 21; x * 2; }); // 42 -
Do Expressions const foo = do { const x =
'b'; const y = 'a'; const z = 'r'; x + y + z; }; console.log(foo); // bar console.log(do { const x = 21; x * 2; }); // 42 Locally scoped variables. -
Do Expressions const foo = do { const x =
'b'; const y = 'a'; const z = 'r'; x + y + z; }; console.log(foo); // bar console.log(do { const x = 21; x * 2; }); // 42 Last statement is return value. -
Do Expressions const foo = do { const x =
'b'; const y = 'a'; const z = 'r'; x + y + z; }; console.log(foo); // bar console.log(do { const x = 21; x * 2; }); // 42 -
Ternary Alternative function readyResponse(ready) { const response = do {
if (ready) "I'm ready. Let's go!"; else "Hold on a minute."; }; console.log(response); } readyResponse(true); // I'm ready. Let's go! readyResponse(false); // Hold on a minute. -
Ternary Alternative function readyResponse(ready) { const response = do {
if (ready) "I'm ready. Let's go!"; else "Hold on a minute."; }; console.log(response); } readyResponse(true); // I'm ready. Let's go! readyResponse(false); // Hold on a minute. Each branch ends with an expression. -
Ternary Alternative const response = ready ? "I'm ready. Let's
go!" : "Hold on a minute."; const response = do { if (ready) "I'm ready. Let's go!"; else "Hold on a minute."; }; Transpile -
Class Decorators • Annotate and modify classes and their properties.
• Use @ and an expression that evaluates to a function. • Decorator precedes a class or method definition. • https://github.com/wycats/javascript-decorators -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } get fullName() { return `${this.firstName} ${this.lastName}`; } } const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } get fullName() { return `${this.firstName} ${this.lastName}`; } } const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } get fullName() { return `${this.firstName} ${this.lastName}`; } } const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank Consequence: getters are nonenumerable by default. -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } } Object.defineProperty(Person.prototype, 'fullName', { enumerable: true, get() { return `${this.firstName} ${this.lastName}`; } }); const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank // fullName: Jeremy Fairbank -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } } Object.defineProperty(Person.prototype, 'fullName', { enumerable: true, get() { return `${this.firstName} ${this.lastName}`; } }); const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank // fullName: Jeremy Fairbank -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } } Object.defineProperty(Person.prototype, 'fullName', { enumerable: true, get() { return `${this.firstName} ${this.lastName}`; } }); const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank // fullName: Jeremy Fairbank -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } @enumerable get fullName() { return `${this.firstName} ${this.lastName}`; } } const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank // fullName: Jeremy Fairbank -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } @enumerable get fullName() { return `${this.firstName} ${this.lastName}`; } } const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank // fullName: Jeremy Fairbank -
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName
= lastName; } @enumerable get fullName() { return `${this.firstName} ${this.lastName}`; } } const p = new Person('Jeremy', 'Fairbank'); for (const key in p) { console.log(`${key}: ${p[key]}`); } // firstName: Jeremy // lastName: Fairbank // fullName: Jeremy Fairbank -
function enumerable(target, name, descriptor) { descriptor.enumerable = true; return descriptor;
} Just Functions Object being modified (e.g. prototype or a class based on usage) -
function enumerable(target, name, descriptor) { descriptor.enumerable = true; return descriptor;
} Just Functions Name of property that will be added to target -
function enumerable(target, name, descriptor) { descriptor.enumerable = true; return descriptor;
} Just Functions Object literal defining the property that will be added to target -
function enumerable(target, name, descriptor) { descriptor.enumerable = true; return descriptor;
} Just Functions Return is optional if you mutate descriptor. Useful if you want to create new descriptor. -
Just Functions @enumerable get fullName() { return `${this.firstName} ${this.lastName}`; }
let descriptor = { get() { return `${this.firstName} ${this.lastName}`; }, enumerable: false, configurable: true }; descriptor = enumerable(Person.prototype, 'fullName', descriptor) || descriptor; Object.defineProperty(Person.prototype, 'fullName', descriptor); -
Just Functions @enumerable get fullName() { return `${this.firstName} ${this.lastName}`; }
let descriptor = { get() { return `${this.firstName} ${this.lastName}`; }, enumerable: false, configurable: true }; descriptor = enumerable(Person.prototype, 'fullName', descriptor) || descriptor; Object.defineProperty(Person.prototype, 'fullName', descriptor); -
Just Functions @enumerable get fullName() { return `${this.firstName} ${this.lastName}`; }
let descriptor = { get() { return `${this.firstName} ${this.lastName}`; }, enumerable: false, configurable: true }; descriptor = enumerable(Person.prototype, 'fullName', descriptor) || descriptor; Object.defineProperty(Person.prototype, 'fullName', descriptor); -
Just Functions @enumerable get fullName() { return `${this.firstName} ${this.lastName}`; }
let descriptor = { get() { return `${this.firstName} ${this.lastName}`; }, enumerable: false, configurable: true }; descriptor = enumerable(Person.prototype, 'fullName', descriptor) || descriptor; Object.defineProperty(Person.prototype, 'fullName', descriptor); -
Decorators and Arguments class Person { constructor(name) { this.name =
name; } @alias('sayHello') greet() { console.log(`Hello. My name is ${this.name}.`); } } const person = new Person('Jeremy'); person.greet(); // Hello. My name is Jeremy. person.sayHello(); // Hello. My name is Jeremy. -
Decorators and Arguments class Person { constructor(name) { this.name =
name; } @alias('sayHello') greet() { console.log(`Hello. My name is ${this.name}.`); } } const person = new Person('Jeremy'); person.greet(); // Hello. My name is Jeremy. person.sayHello(); // Hello. My name is Jeremy. Evaluate function call and use return value as decorator. -
Decorators and Arguments class Person { constructor(name) { this.name =
name; } @alias('sayHello') greet() { console.log(`Hello. My name is ${this.name}.`); } } const person = new Person('Jeremy'); person.greet(); // Hello. My name is Jeremy. person.sayHello(); // Hello. My name is Jeremy. -
Decorators with Parameters function alias(aliasName) { return (target, name, descriptor)
=> { const newDescriptor = Object.assign({}, descriptor, { value(...args) { return this[name](...args); } }); Object.defineProperty(target, aliasName, newDescriptor); }; } -
Decorators with Parameters function alias(aliasName) { return (target, name, descriptor)
=> { const newDescriptor = Object.assign({}, descriptor, { value(...args) { return this[name](...args); } }); Object.defineProperty(target, aliasName, newDescriptor); }; } Take a parameter -
Decorators with Parameters function alias(aliasName) { return (target, name, descriptor)
=> { const newDescriptor = Object.assign({}, descriptor, { value(...args) { return this[name](...args); } }); Object.defineProperty(target, aliasName, newDescriptor); }; } Actual decorator function -
const Quadrupedal = { walk() { console.log(`${this.name} walks with four
legs`); } }; @mixin(Quadrupedal, EventEmitter.prototype) class Dog { constructor(name, age) { this.name = name; this.age = age; } haveBirthday() { this.age++; this.emit('birthday', this); } } -
const Quadrupedal = { walk() { console.log(`${this.name} walks with four
legs`); } }; @mixin(Quadrupedal, EventEmitter.prototype) class Dog { constructor(name, age) { this.name = name; this.age = age; } haveBirthday() { this.age++; this.emit('birthday', this); } } Use a decorator on the class -
const Quadrupedal = { walk() { console.log(`${this.name} walks with four
legs`); } }; @mixin(Quadrupedal, EventEmitter.prototype) class Dog { constructor(name, age) { this.name = name; this.age = age; } haveBirthday() { this.age++; this.emit('birthday', this); } } -
const dog = new Dog('Tucker', 8); dog.on('birthday', d => {
console.log(`${d.name} just turned ${d.age}`); }); dog.haveBirthday(); // Tucker just turned 9 dog.walk(); // Tucker walks with four legs -
const dog = new Dog('Tucker', 8); dog.on('birthday', d => {
console.log(`${d.name} just turned ${d.age}`); }); dog.haveBirthday(); // Tucker just turned 9 dog.walk(); // Tucker walks with four legs From EventEmitter From Quadrupedal -
Why Class Decorators? • Extremely powerful and expressive syntax for
class/property modification. • Mixins — no more fragile base class or multiple inheritance issues. • @autobind, @deprecate, @memoize, @readonly, and more! • https://github.com/jayphelps/core-decorators.js -
Async Functions • Provide synchronous-like syntax for asynchronous code. •
Prepend function with async. • Use await operator on a promise to obtain a fulfilled promise value. • https://github.com/tc39/ecmascript-asyncawait -
function loadOrder(orderId) { fetchJson(`/orders/${orderId}`) .then(order => console.log(order)); } async function
loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order); } loadOrder(1); -
async function loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order);
} loadOrder(1); Declare a function as async -
async function loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order);
} loadOrder(1); Await a promise and obtain the fulfilled/resolved value. Nonblocking. -
async function loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order);
} loadOrder(1); Invoke function. -
async function loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order);
} loadOrder(1); Encounter await. -
async function loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order);
} loadOrder(1); Wrap awaited expression in Promise. -
Wrap awaited expression in Promise. async function loadOrder(orderId) { const
promise = Promise.resolve( fetchJson(`/orders/${orderId}`) ); console.log(order); } loadOrder(1); -
Wrap remaining code in a then callback. async function loadOrder(orderId)
{ const promise = Promise.resolve( fetchJson(`/orders/${orderId}`) ); console.log(order); } loadOrder(1); -
Wrap remaining code in a then callback. function loadOrder(orderId) {
const promise = Promise.resolve( fetchJson(`/orders/${orderId}`) ); return promise.then(order => { console.log(order); return Promise.resolve(undefined); }); } loadOrder(1); -
No explicit return, so return a resolved undefined value. function
loadOrder(orderId) { const promise = Promise.resolve( fetchJson(`/orders/${orderId}`) ); return promise.then(order => { console.log(order); return Promise.resolve(undefined); }); } loadOrder(1); -
async function loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order);
} function loadOrder(orderId) { const promise = Promise.resolve( fetchJson(`/orders/${orderId}`) ); return promise.then(order => { console.log(order); return Promise.resolve(undefined); }); } Final equivalent-ish code -
async function loadOrder(orderId) { const order = await fetchJson(`/orders/${orderId}`); console.log(order);
} function loadOrder(orderId) { const promise = Promise.resolve( fetchJson(`/orders/${orderId}`) ); return promise.then(order => { console.log(order); return Promise.resolve(undefined); }); } What does this function look like? -
Multiple Awaits async function fetchJson(url) { const resp = await
fetch(url); const data = await resp.json(); const isSuccess = resp.status >= 200 && resp.status < 300; if (isSuccess) { return data; } const error = new Error(resp.responseText); error.data = data; throw error; } -
Multiple Awaits async function fetchJson(url) { const resp = await
fetch(url); const data = await resp.json(); const isSuccess = resp.status >= 200 && resp.status < 300; if (isSuccess) { return data; } const error = new Error(resp.responseText); error.data = data; throw error; } Potential for dependency means sequential awaits are NOT parallel! -
Multiple Awaits async function fetchJson(url) { const resp = await
fetch(url); const data = await resp.json(); const isSuccess = resp.status >= 200 && resp.status < 300; if (isSuccess) { return data; } const error = new Error(resp.responseText); error.data = data; throw error; } -
Multiple Awaits async function fetchJson(url) { const resp = await
fetch(url); const data = await resp.json(); const isSuccess = resp.status >= 200 && resp.status < 300; if (isSuccess) { return data; } const error = new Error(resp.responseText); error.data = data; throw error; } What will happen? -
Error Handling async function loadOrder(orderId) { try { const order
= await fetchJson(`/orders/${orderId}`); console.log(order); } catch(e) { console.log('error retrieving order', e.data); } } loadOrder(1); -
Error Handling async function loadOrder(orderId) { try { const order
= await fetchJson(`/orders/${orderId}`); console.log(order); } catch(e) { console.log('error retrieving order', e.data); } } loadOrder(1); Promise was resolved -
Error Handling async function loadOrder(orderId) { try { const order
= await fetchJson(`/orders/${orderId}`); console.log(order); } catch(e) { console.log('error retrieving order', e.data); } } loadOrder(1); Promise was rejected -
function loadOrder(orderId) { fetchJson(`/orders/${orderId}`) .then(order => console.log(order)) .catch(e => console.log('error
retrieving order', e.data)); } async function loadOrder(orderId) { try { const order = await fetchJson(`/orders/${orderId}`); console.log(order); } catch(e) { console.log('error retrieving order', e.data); } } -
function loadOrder(orderId) { fetchJson(`/orders/${orderId}`) .then(order => console.log(order)) .catch(e => console.log('error
retrieving order', e.data)); } async function loadOrder(orderId) { try { const order = await fetchJson(`/orders/${orderId}`); console.log(order); } catch(e) { console.log('error retrieving order', e.data); } } -
function loadOrder(orderId) { fetchJson(`/orders/${orderId}`) .then(order => console.log(order)) .catch(e => console.log('error
retrieving order', e.data)); } async function loadOrder(orderId) { try { const order = await fetchJson(`/orders/${orderId}`); console.log(order); } catch(e) { console.log('error retrieving order', e.data); } } -
function loadOrder(orderId) { fetchJson(`/orders/${orderId}`) .then(order => console.log(order)) .catch(e => console.log('error
retrieving order', e.data)); } async function loadOrder(orderId) { try { const order = await fetchJson(`/orders/${orderId}`); console.log(order); } catch(e) { console.log('error retrieving order', e.data); } } -
Explicit Returns async function life() { return 42; } const
answer = life(); -
Explicit Returns async function life() { return 42; } const
answer = life(); What is the value of answer? -
Explicit Returns async function life() { return 42; } const
answer = life(); assert(answer !== 42); assert(answer instanceof Promise); answer.then(value => assert(value === 42)); -
Explicit Returns async function life() { return 42; } Remember
that async functions wrap the return value with Promise.resolve. function life() { return Promise.resolve(42); } -
Sequential vs. Parallel async function loadOrders(orderIds) { const orders =
[]; for (const id of orderIds) { const order = await fetchJson(`/orders/${id}`); orders.push(order); } console.log(orders); } loadOrders([1, 2, 3]); -
Sequential vs. Parallel async function loadOrders(orderIds) { const orders =
[]; for (const id of orderIds) { const order = await fetchJson(`/orders/${id}`); orders.push(order); } console.log(orders); } loadOrders([1, 2, 3]); Each step of loop has to wait. Issue if requests aren’t dependent on each other. -
async function loadOrders(orderIds) { const orders = await Promise.all(orderIds.map(id =>
{ return fetchJson(`/orders/${id}`); })); console.log(orders); } loadOrders([1, 2, 3]); Sequential vs. Parallel -
Sequential vs. Parallel async function loadOrders(orderIds) { const orders =
await Promise.all(orderIds.map(id => { return fetchJson(`/orders/${id}`); })); console.log(orders); } loadOrders([1, 2, 3]); Make all requests at once and resolve with Promise.all. Allows for parallelism. -
Sequential Queue Request /orders/1 (wait: 0s) Requests Request /orders/2 (wait:
0s) Request /orders/3 (wait: 0s) Finished Time Total: 0s -
Sequential Queue Requests Request /orders/3 (wait: 0s) Finished Request /orders/1
(wait: 0s) Time Total: 0s Request /orders/2 (wait: 0s) -
Sequential Queue Requests Finished Request /orders/1 (wait: 0s, resp: 1s)
Time Total: 1s Request /orders/2 (wait: 1s) Request /orders/3 (wait: 1s) -
Sequential Queue Requests Finished Request /orders/1 (wait: 0s, resp: 1s)
Time Total: 1s Request /orders/2 (wait: 1s) Request /orders/3 (wait: 1s) -
Sequential Queue Requests Finished Request /orders/1 (wait: 0s, resp: 1s)
Time Total: 7s Request /orders/2 (wait: 1s, resp: 6s) Request /orders/3 (wait: 7s) -
Sequential Queue Requests Request /orders/3 (wait: 7s) Finished Request /orders/1
(wait: 0s, resp: 1s) Time Total: 7s Request /orders/2 (wait: 1s, resp: 6s) -
Sequential Queue Requests Request /orders/3 (wait: 7s, resp: 2s) Finished
Request /orders/1 (wait: 0s, resp: 1s) Time Total: 9s Request /orders/2 (wait: 1s, resp: 6s) -
Sequential Queue Requests Request /orders/3 (wait: 7s, resp: 2s) Finished
Request /orders/1 (wait: 0s, resp: 1s) Time Total: 9s Request /orders/2 (wait: 1s, resp: 6s) Limited by response time of every request. -
Parallel Queue Request /orders/1 (wait: 0s) Requests Request /orders/2 (wait:
0s) Request /orders/3 (wait: 0s) Finished Time Total: 0s -
Parallel Queue Request /orders/1 (wait: 0s) Requests Request /orders/2 (wait:
0s) Request /orders/3 (wait: 0s) Finished Time Total: 0s -
Parallel Queue Request /orders/2 (wait: 0s) Requests Request /orders/3 (wait:
0s) Finished Time Total: 1s Request /orders/1 (wait: 0s, resp: 1s) -
Parallel Queue Request /orders/2 (wait: 0s) Requests Request /orders/3 (wait:
0s, resp: 2s) Finished Time Total: 2s Request /orders/1 (wait: 0s, resp: 1s) -
Parallel Queue Request /orders/2 (wait: 0s, resp: 6s) Requests Request
/orders/3 (wait: 0s, resp: 2s) Finished Time Total: 6s Request /orders/1 (wait: 0s, resp: 1s) -
Parallel Queue Request /orders/2 (wait: 0s, resp: 6s) Requests Request
/orders/3 (wait: 0s, resp: 2s) Finished Time Total: 6s Request /orders/1 (wait: 0s, resp: 1s) Limited by slowest request instead of every request. -
Why Async Functions? • Synchronous-looking asynchronous code. • Gain back
use of native language flow control constructs with asynchronous code (e.g. for..of, try/catch). -
Other upcoming features • Stage 3: Exponentiation operator, Array.prototype.includes •
Stage 2: Object.observe, SIMD • Stage 1: Object rest and spread properties, typed objects, Observable • Stage 0: Shared memory and atomics, class property initializers • Many more! -
Resources • https://github.com/tc39/ecma262 • http://babeljs.io/ • $ babel --stage 0
myAwesomeES7code.js • https://tc39.github.io/process-document/ • https://esdiscuss.org/ • http://www.ecma-international.org/memento/ contribute_TC39_Royalty_Free_Task_Group.php