SlideShare a Scribd company logo
ES.next for humans
@janjongboom
LvivJS 2014
@janjongboom
@janjongboom
I hate
JavaScript!
VBScript
<3 <3 <3
програма
Things you could already do
Things you needed a framework for
Things that were impossible
Things you can already do
alt: S*#t that annoys Jan in JavaScript
NodeList is not an array
1 document.querySelectorAll('li')
2 .filter(function(li) {
3 /* do something */
4 });
5
ERROR: document.querySelectorAll(...).filter
is not a function
NodeList is not an array
1 Array.prototype.slice.call(
2 document.querySelectorAll('li')
3 )
NodeList is not an array
1 Array.prototype.slice.call(
2 document.querySelectorAll('li')
3 )
1 Array.from(
2 document.querySelectorAll('li')
3 )
Variable scoping
1 var a = 7;
2
3 if (something) {
4 var a = 9;
5 doSomethingElse(a);
6 }
7
8 console.log(a);
Variable scoping
1 var a = 7;
2
3 if (something) {
4 var a = 9;
5 doSomethingElse(a);
6 }
7
8 console.log(a);
// can be 7 or 9 depending on 'something'
Variable scoping
1 var a = 7;
2
3 if (something) {
4 var a = 9;
5 doSomethingElse(a);
6 }
7
8 console.log(a);
// can be 7 or 9 depending on 'something'
Variable scoping
1 var a = 7;
2
3 if (something) {
4 var a = 9;
5 doSomethingElse(a);
6 }
7
8 console.log(a);
// can be 7 or 9 depending on 'something'
Variable scoping
1 let a = 7;
2
3 if (something) {
4 let a = 9;
5 doSomethingElse(a);
6 }
7
8 console.log(a);
// always 7
‘let’ to the rescue
Variable scoping
1 let a = 7;
2
3 if (something) {
4 let a = 9;
5 doSomethingElse(a);
6 }
7
8 console.log(a);
// always 7
‘let’ to the rescue
Tuples
Multiple return values
1 function getPosition() {
2 return [ 12, 91 ];
3 }
4
5 var pos = getPosition();
6 var x = pos[0],
7 y = pos[1];
8
9 function getPosition() {
10 return { x: 12, y: 91 };
11 }
12
13 var pos = getPosition();
14 var x = pos.x;
15 y = pos.y;
1 function getPosition() {
2 return [ 12, 91 ];
3 }
4
5 var pos = getPosition();
6 var x = pos[0],
7 y = pos[1];
8
9 function getPosition() {
10 return { x: 12, y: 91 };
11 }
12
13 var pos = getPosition();
14 var x = pos.x;
15 y = pos.y;
let [x, y] = getPosition();
1 function getPosition() {
2 return [ 12, 91 ];
3 }
4
5 var pos = getPosition();
6 var x = pos[0],
7 y = pos[1];
8
9 function getPosition() {
10 return { x: 12, y: 91 };
11 }
12
13 var pos = getPosition();
14 var x = pos.x;
15 y = pos.y;
let [x, y] = getPosition();
let {x, y} = getPosition();
1 var data = {};
2
3 data['jan'] = 'awesome';
4 data['pete'] = 'not so awesome';
5
6 var ele1 = document.querySelector('#el1');
7 data[ele1] = 'first element';
8
9 var ele2 = document.querySelector('#el2');
10 data[ele2] = 'second element';
11
12 console.log(data[ele1]);
Maps
1 var data = {};
2
3 data['jan'] = 'awesome';
4 data['pete'] = 'not so awesome';
5
6 var ele1 = document.querySelector('#el1');
7 data[ele1] = 'first element';
8
9 var ele2 = document.querySelector('#el2');
10 data[ele2] = 'second element';
11
12 console.log(data[ele1]);
Maps
1 var data = {};
2
3 data['jan'] = 'awesome';
4 data['pete'] = 'not so awesome';
5
6 var ele1 = document.querySelector('#el1');
7 data[ele1] = 'first element';
8
9 var ele2 = document.querySelector('#el2');
10 data[ele2] = 'second element';
11
12 console.log(data[ele1]);
Maps
“second element”
1 var data = {};
2
3 data['jan'] = 'awesome';
4 data['pete'] = 'not so awesome';
5
6 var ele1 = document.querySelector('#el1');
7 data[ele1] = 'first element';
8
9 var ele2 = document.querySelector('#el2');
10 data[ele2] = 'second element';
11
12 console.log(data[ele1]);
Maps
Maps
1 var data = {};
2
3 data['jan'] = 'awesome';
4 data['pete'] = 'not so awesome';
5
6 var ele1 = document.querySelector('#el1');
7 data[ele1] = 'first element';
8
9 var ele2 = document.querySelector('#el2');
10 data[ele2] = 'second element';
11
12 console.log(data[ele1]);
Maps
1 var data = {};
2
3 data['jan'] = 'awesome';
4 data['pete'] = 'not so awesome';
5
6 var ele1 = document.querySelector('#el1');
7 data[ele1] = 'first element';
8
9 var ele2 = document.querySelector('#el2');
10 data[ele2] = 'second element';
11
12 console.log(data[ele1]);
data[ele2.toString()]
ele2.toString() =>
“[object HTMLDivElement”]
Maps
1 var data = new Map();
2
3 data.set('jan', 'awesome');
4
5 data.has('jan'); // true
6 data.get('jan'); // "awesome"
7
8 var ele1 = document.querySelector('#el1');
9 data.set(ele1, 'first element');
10
11 var ele2 = document.querySelector('#el2');
12 data.set(ele2, 'second element');
13
14 console.log(data.get(ele1)); // "first element"
Maps
1 var data = new Map();
2
3 data.set('jan', 'awesome');
4
5 data.has('jan'); // true
6 data.get('jan'); // "awesome"
7
8 var ele1 = document.querySelector('#el1');
9 data.set(ele1, 'first element');
10
11 var ele2 = document.querySelector('#el2');
12 data.set(ele2, 'second element');
13
14 console.log(data.get(ele1)); // "first element"
Function boilerplate
1
2 someArray
3 .filter(function(item) {
4 return item.isActive;
5 })
6 .map(function(item) {
7 return item.id;
8 });
9
10 setTimeout(function() {
11 doSomeMagic()
12 }, 500);
Function boilerplate
1
2 someArray
3 .filter(function(item) {
4 return item.isActive;
5 })
6 .map(function(item) {
7 return item.id;
8 });
9
10 setTimeout(function() {
11 doSomeMagic()
12 }, 500);
.filter(item => item.isActive)
Function boilerplate
1
2 someArray
3 .filter(function(item) {
4 return item.isActive;
5 })
6 .map(function(item) {
7 return item.id;
8 });
9
10 setTimeout(function() {
11 doSomeMagic()
12 }, 500);
.filter(item => item.isActive)
.map(item => item.id);
Function boilerplate
1
2 someArray
3 .filter(function(item) {
4 return item.isActive;
5 })
6 .map(function(item) {
7 return item.id;
8 });
9
10 setTimeout(function() {
11 doSomeMagic()
12 }, 500);
.filter(item => item.isActive)
.map(item => item.id);
setTimeout(() => {
Things you need a
framework for
a.k.a. Goodbye Angular!
Promises
1 new Promise(function(resolve, reject) {
2 resolve('Success!');
3 // or...
4 reject('I failed!');
5 });
Native promises
Data binding
AngularJS
1 function SomeCtrl() {
2 $scope.name = "Jan";
3 }
1 <div ng-controller="SomeCtrl">
2 <input type="text" ng-model="name">
3
4 Hi {{name}}
5 </div>
Now without
a framework
From JS -> HTML
1 <div>
2 <input type="text" data-bind="name">
3 Hello <span data-bind="name"></span>
4 </div>
1 var scope = {
2 name: 'Jan'
3 };
4 // ???????
From JS -> HTML
1 <div>
2 <input type="text" data-bind="name">
3 Hello <span data-bind="name"></span>
4 </div>
1 var scope = {
2 name: 'Jan'
3 };
4 // ???????
From JS -> HTML
1 <div>
2 <input type="text" data-bind="name">
3 Hello <span data-bind="name"></span>
4 </div>
1 var scope = {
2 name: 'Jan'
3 };
4 // ???????
1 function updateScope() {
2 var els = document.querySelectorAll(
3 '*[data-bind=name]'
4 );
5
6 Array.from(els).forEach(function(el) {
7 if ('value' in el) // input element
8 el.value = scope.name;
9 else if ('textContent' in el) // normal
10 el.textContent = scope.name;
11 });
12 }
13
14 updateScope();
1 function updateScope() {
2 var els = document.querySelectorAll(
3 '*[data-bind=name]'
4 );
5
6 Array.from(els).forEach(function(el) {
7 if ('value' in el) // input element
8 el.value = scope.name;
9 else if ('textContent' in el) // normal
10 el.textContent = scope.name;
11 });
12 }
13
14 updateScope();
1 function updateScope() {
2 var els = document.querySelectorAll(
3 '*[data-bind=name]'
4 );
5
6 Array.from(els).forEach(function(el) {
7 if ('value' in el) // input element
8 el.value = scope.name;
9 else if ('textContent' in el) // normal
10 el.textContent = scope.name;
11 });
12 }
13
14 updateScope();
1 function updateScope() {
2 var els = document.querySelectorAll(
3 '*[data-bind=name]'
4 );
5
6 Array.from(els).forEach(function(el) {
7 if ('value' in el) // input element
8 el.value = scope.name;
9 else if ('textContent' in el) // normal
10 el.textContent = scope.name;
11 });
12 }
13
14 updateScope();
1 function updateScope() {
2 var els = document.querySelectorAll(
3 '*[data-bind=name]'
4 );
5
6 Array.from(els).forEach(function(el) {
7 if ('value' in el) // input element
8 el.value = scope.name;
9 else if ('textContent' in el) // normal
10 el.textContent = scope.name;
11 });
12 }
13
14 updateScope();
1 <div>
2 <input type="text" data-bind="name">
3 Hello <span data-bind="name"></span>
4 </div>
But what if we change
scope.name from JS?
1 Object.observe(scope, function(changes) {
2 changes.forEach(function(change) {
3 if (change.type === 'update' &&
4 change.name === 'name') {
5 updateScope();
6 }
7 });
8 });
9
10
11 setTimeout(function() {
12 scope.name = 'Vladimir';
13 }, 2000);
1 Object.observe(scope, function(changes) {
2 changes.forEach(function(change) {
3 if (change.type === 'update' &&
4 change.name === 'name') {
5 updateScope();
6 }
7 });
8 });
9
10
11 setTimeout(function() {
12 scope.name = 'Vladimir';
13 }, 2000);
1 Object.observe(scope, function(changes) {
2 changes.forEach(function(change) {
3 if (change.type === 'update' &&
4 change.name === 'name') {
5 updateScope();
6 }
7 });
8 });
9
10
11 setTimeout(function() {
12 scope.name = 'Vladimir';
13 }, 2000);
{ type: "update", name: "name", oldValue: "Jan"}
1 Object.observe(scope, function(changes) {
2 changes.forEach(function(change) {
3 if (change.type === 'update' &&
4 change.name === 'name') {
5 updateScope();
6 }
7 });
8 });
9
10
11 setTimeout(function() {
12 scope.name = 'Vladimir';
13 }, 2000);
{ type: "update", name: "name", oldValue: "Jan"}
From HTML -> JS (1)
1 var els = document.querySelectorAll(
2 '*[data-bind=name]'
3 );
4
5 var observer = new MutationObserver(function() {
6 scope.name = this.textContent;
7 });
8
9 Array.from(els).forEach(function(el) {
10 observer.observe(el, { childList: true });
11 });
From HTML -> JS (1)
1 var els = document.querySelectorAll(
2 '*[data-bind=name]'
3 );
4
5 var observer = new MutationObserver(function() {
6 scope.name = this.textContent;
7 });
8
9 Array.from(els).forEach(function(el) {
10 observer.observe(el, { childList: true });
11 });
From HTML to JS (2)
1 var els = document.querySelectorAll(
2 '*[data-bind=name]'
3 );
4
5 Array.from(els).forEach(function(el) {
6 el.onkeyup = function() {
7 if (el.value)
8 scope.name = el.value;
9 }
10 });
From HTML to JS (2)
1 var els = document.querySelectorAll(
2 '*[data-bind=name]'
3 );
4
5 Array.from(els).forEach(function(el) {
6 el.onkeyup = function() {
7 if (el.value)
8 scope.name = el.value;
9 }
10 });
OMG AWESOME WTF
APESHIT INSANE
Things you cannot do
a.k.a. I want it now!
Proxies
Intercepting calls to objects with
JS Object
{ id: 4, city: "Lviv" }
JS Object
{ id: 4, city: "Lviv" }
Application code
JS Object
{ id: 4, city: "Lviv" }
Application code
alert(obj.id)
JS Object
{ id: 4, city: "Lviv" }
Application code
alert(obj.id)
obj.city = "Kiev"
Proxy
JS Object
{ id: 4, city: "Lviv" }
Application code
alert(obj.id)
obj.city = "Kiev"
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 console.log('Get for', name);
9 return target[name];
10 }
11 }
12 );
13
14 alert(obj.city);
15 // Get for 'city'
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 console.log('Get for', name);
9 return target[name];
10 }
11 }
12 );
13
14 alert(obj.city);
15 // Get for 'city'
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 console.log('Get for', name);
9 return target[name];
10 }
11 }
12 );
13
14 alert(obj.city);
15 // Get for 'city'
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 console.log('Get for', name);
9 return target[name];
10 }
11 }
12 );
13
14 alert(obj.city);
15 // Get for 'city'
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 if (name === 'city') {
9 return 'Kiev';
10 }
11 return target[name];
12 }
13 }
14 );
15
16 console.log(obj.city);
17 // returns 'Kiev' ?!!
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 if (name === 'city') {
9 return 'Kiev';
10 }
11 return target[name];
12 }
13 }
14 );
15
16 console.log(obj.city);
17 // returns 'Kiev' ?!!
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 if (!(name in target)) {
9 throw 'has no property "' + name + '"'
10 }
11 return target[name];
12 }
13 }
14 );
15
16 console.log(obj.citi);
ERROR: has no property 'citi'
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 get: function(target, name) {
8 if (!(name in target)) {
9 throw 'has no property "' + name + '"'
10 }
11 return target[name];
12 }
13 }
14 );
15
16 console.log(obj.citi);
ERROR: has no property 'citi'
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 set: function(target, name, value) {
8 if (name === 'id'
9 && typeof value !== 'number')
10 throw 'id must be numeric';
11 target[name] = value;
12 }
13 }
14 );
15
16 obj.id = '5';
ERROR: id must be numeric
1 var obj = new Proxy(
2 {
3 id: 4,
4 city: "Lviv"
5 },
6 {
7 set: function(target, name, value) {
8 if (name === 'id'
9 && typeof value !== 'number')
10 throw 'id must be numeric';
11 target[name] = value;
12 }
13 }
14 );
15
16 obj.id = '5';
ERROR: id must be numeric
1 set: function(target, name, value) {
2 if (target[name] !== value) {
3 console.log('Value for', name, 'changed');
4 }
5 }
Proxies are cool
Aspect Oriented Programming
Logging
Access Control
Generators
Lazy functions
Generators
Lazy functions
Normal function
1 function normal() {
2 console.log('Hi there!');
3
4 var a = 5 + 6;
5 console.log('I think it's', a);
6
7 console.log('kthxbye');
8
9 return 3;
10 }
Generators are lazy
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 turingWinners();
Generators are lazy
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 turingWinners();
Generators are lazy
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 turingWinners();
Generators are lazy
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 turingWinners();
Generators are lazy
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 turingWinners();
$ node --harmony test.js
$
Call next() to start
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 var iterator = turingWinners();
11
12 console.log(iterator.next());
13 console.log(iterator.next());
Call next() to start
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 var iterator = turingWinners();
11
12 console.log(iterator.next());
13 console.log(iterator.next());
Call next() to start
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 var iterator = turingWinners();
11
12 console.log(iterator.next());
13 console.log(iterator.next());
Call next() to start
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 var iterator = turingWinners();
11
12 console.log(iterator.next());
13 console.log(iterator.next());
$ node --harmony test.js
Hello from our function
{ value: 'Alan J. Perlis', done: false }
Call next() to start
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 var iterator = turingWinners();
11
12 console.log(iterator.next());
13 console.log(iterator.next());
Call next() to start
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 var iterator = turingWinners();
11
12 console.log(iterator.next());
13 console.log(iterator.next());
Call next() to start
1 function* turingWinners () {
2 console.log('Hello from our function');
3 yield "Alan J. Perlis";
4 console.log('I returned the first one');
5 yield "Maurice Wilkes";
6 yield "Richard Hamming";
7 yield "Marvin Minsky";
8 }
9
10 var iterator = turingWinners();
11
12 console.log(iterator.next());
13 console.log(iterator.next());
$ node --harmony test.js
Hello from our function
{ value: 'Alan J. Perlis', done: false }
I returned the first one
{ value: 'Maurice Wilkes', done: false }
Inef!cient thing in JS
1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12]
2 .filter(function(v) {
3 return v % 2 === 0;
4 })
5 .map(function(v) {
6 return v * v;
7 })
8 .slice(0, 3);
9
10 console.log(nice);
Inef!cient thing in JS
1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12]
2 .filter(function(v) {
3 return v % 2 === 0;
4 })
5 .map(function(v) {
6 return v * v;
7 })
8 .slice(0, 3);
9
10 console.log(nice);
Inef!cient thing in JS
1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12]
2 .filter(function(v) {
3 return v % 2 === 0;
4 })
5 .map(function(v) {
6 return v * v;
7 })
8 .slice(0, 3);
9
10 console.log(nice);
Inef!cient thing in JS
1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12]
2 .filter(function(v) {
3 return v % 2 === 0;
4 })
5 .map(function(v) {
6 return v * v;
7 })
8 .slice(0, 3);
9
10 console.log(nice);
Inef!cient thing in JS
1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12]
2 .filter(function(v) {
3 return v % 2 === 0;
4 })
5 .map(function(v) {
6 return v * v;
7 })
8 .slice(0, 3);
9
10 console.log(nice);
1 function* generateNumber() {
2 var i = 0;
3 while (true)
4 yield ++i;
5 }
6
7 function* filter(it) {
8 for (var n of it)
9 if (n % 2 == 0) yield curr;
10 }
11
12 function* map(it) {
13 for (var n of it)
14 yield n * n;
15 }
1 function* generateNumber() {
2 var i = 0;
3 while (true)
4 yield ++i;
5 }
6
7 function* filter(it) {
8 for (var n of it)
9 if (n % 2 == 0) yield curr;
10 }
11
12 function* map(it) {
13 for (var n of it)
14 yield n * n;
15 }
1 function* generateNumber() {
2 var i = 0;
3 while (true)
4 yield ++i;
5 }
6
7 function* filter(it) {
8 for (var n of it)
9 if (n % 2 == 0) yield curr;
10 }
11
12 function* map(it) {
13 for (var n of it)
14 yield n * n;
15 }
1,2,3,4,5,6
1 function* generateNumber() {
2 var i = 0;
3 while (true)
4 yield ++i;
5 }
6
7 function* filter(it) {
8 for (var n of it)
9 if (n % 2 == 0) yield curr;
10 }
11
12 function* map(it) {
13 for (var n of it)
14 yield n * n;
15 }
1,2,3,4,5,6
1 function* generateNumber() {
2 var i = 0;
3 while (true)
4 yield ++i;
5 }
6
7 function* filter(it) {
8 for (var n of it)
9 if (n % 2 == 0) yield curr;
10 }
11
12 function* map(it) {
13 for (var n of it)
14 yield n * n;
15 }
1,2,3,4,5,6
n%2 == 0
2,4,6
1 function* generateNumber() {
2 var i = 0;
3 while (true)
4 yield ++i;
5 }
6
7 function* filter(it) {
8 for (var n of it)
9 if (n % 2 == 0) yield curr;
10 }
11
12 function* map(it) {
13 for (var n of it)
14 yield n * n;
15 }
1,2,3,4,5,6
n%2 == 0
2,4,6
1 function* generateNumber() {
2 var i = 0;
3 while (true)
4 yield ++i;
5 }
6
7 function* filter(it) {
8 for (var n of it)
9 if (n % 2 == 0) yield curr;
10 }
11
12 function* map(it) {
13 for (var n of it)
14 yield n * n;
15 }
1,2,3,4,5,6
n%2 == 0
2,4,6
n * n
4,16,36
Using the generators
1 var nice = generateNumber();
2 nice = filter(nice);
3 nice = map(nice);
4
5 for (var i = 0; i < 3; i++) {
6 console.log(i, nice.next().value);
7 }
Using the generators
1 var nice = generateNumber();
2 nice = filter(nice);
3 nice = map(nice);
4
5 for (var i = 0; i < 3; i++) {
6 console.log(i, nice.next().value);
7 }
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
$ node --harmony test.js
It: Whats your name?
Two way interaction
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
5
6 var iterator = printName();
7 console.log('It:', iterator.next().value);
8
9 setTimeout(function() {
10 var ret = iterator.next('Jan Jongboom');
11 console.log(ret);
12 }, 1000);
$ node --harmony test.js
It: Whats your name?
$ node --harmony test.js
It: Whats your name?
Hello Jan Jongboom
{ value: undefined, done: true }
Yield and deferred values
• Wrote sync code
• But yield waits until new value comes in...
• So it’s actually async with sync syntax
• We need to abuse this!
1 function* printName() {
2 var name = yield 'Whats your name?';
3 console.log('Hello', name);
4 }
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
1 function* run() {
2 yield sleep(2000);
3 console.log('It has been 2 seconds');
4 }
5
6 function sleep(ms) {
7 return new Promise((res, rej) => {
8 setTimeout(res, ms);
9 });
10 }
11
12 var it = run();
13 var ret = it.next().value;
14
15 ret.then(function() {
16 it.next();
17 });
http://pag.forbeslindesay.co.uk/#/22
1 run(function* () {
2 var data = yield $.get('/yolo');
3 data = JSON.parse(data);
4 });
http://pag.forbeslindesay.co.uk/#/22
1 run(function* () {
2 try {
3 var moar = yield $.post('/other');
4 }
5 catch (ex) {
6 console.error('Oh noes!', ex);
7 }
8 });
http://pag.forbeslindesay.co.uk/#/22
1 run(function* () {
2 var req1 = $.get('http://a');
3 var req2 = $.get('http://b');
4
5 $('.status').text(
yield req1 + yield req2);
6 });
OMGAWESOME
Thank you!
slideshare.net/janjongboom
slideshare.net/janjongboom
Questions?

More Related Content

What's hot

Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1
Zianed Hou
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
Pierre MARTIN
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8
Wilson Su
 
Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8
Wilson Su
 
PostgreSQL Open SV 2018
PostgreSQL Open SV 2018PostgreSQL Open SV 2018
PostgreSQL Open SV 2018
artgillespie
 
Specs2
Specs2Specs2
Create a Customized GMF DnD Framework
Create a Customized GMF DnD FrameworkCreate a Customized GMF DnD Framework
Create a Customized GMF DnD FrameworkKaniska Mandal
 
JPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream APIJPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream API
tvaleev
 
Practical JavaScript Programming - Session 5/8
Practical JavaScript Programming - Session 5/8Practical JavaScript Programming - Session 5/8
Practical JavaScript Programming - Session 5/8
Wilson Su
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
Wilson Su
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
David Rodenas
 
Entity Framework Core: tips and tricks
Entity Framework Core: tips and tricksEntity Framework Core: tips and tricks
Entity Framework Core: tips and tricks
ArturDr
 
JDBC Core Concept
JDBC Core ConceptJDBC Core Concept
JDBC Core Concept
Rays Technologies
 
Google guava
Google guavaGoogle guava
H base programming
H base programmingH base programming
H base programming
Muthusamy Manigandan
 
#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG
Thierry Wasylczenko
 
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorProgramming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Fedor Lavrentyev
 

What's hot (20)

Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8
 
Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8
 
PostgreSQL Open SV 2018
PostgreSQL Open SV 2018PostgreSQL Open SV 2018
PostgreSQL Open SV 2018
 
Specs2
Specs2Specs2
Specs2
 
Create a Customized GMF DnD Framework
Create a Customized GMF DnD FrameworkCreate a Customized GMF DnD Framework
Create a Customized GMF DnD Framework
 
JPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream APIJPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream API
 
Practical JavaScript Programming - Session 5/8
Practical JavaScript Programming - Session 5/8Practical JavaScript Programming - Session 5/8
Practical JavaScript Programming - Session 5/8
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
 
XTW_Import
XTW_ImportXTW_Import
XTW_Import
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
 
Entity Framework Core: tips and tricks
Entity Framework Core: tips and tricksEntity Framework Core: tips and tricks
Entity Framework Core: tips and tricks
 
JDBC Core Concept
JDBC Core ConceptJDBC Core Concept
JDBC Core Concept
 
Easy Button
Easy ButtonEasy Button
Easy Button
 
Google guava
Google guavaGoogle guava
Google guava
 
H base programming
H base programmingH base programming
H base programming
 
#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG
 
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorProgramming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
 

Viewers also liked

Firefox OS Apps and Web APIs
Firefox OS Apps and Web APIsFirefox OS Apps and Web APIs
Firefox OS Apps and Web APIs
Jan Jongboom
 
Firefox OS and the Internet of Things - NDC London 2014
Firefox OS and the Internet of Things - NDC London 2014Firefox OS and the Internet of Things - NDC London 2014
Firefox OS and the Internet of Things - NDC London 2014
Jan Jongboom
 
Massive applications in node.js
Massive applications in node.jsMassive applications in node.js
Massive applications in node.js
Jan Jongboom
 
Run your JavaScript app for years on a coin cell - JSConf.asia 2016
Run your JavaScript app for years on a coin cell - JSConf.asia 2016Run your JavaScript app for years on a coin cell - JSConf.asia 2016
Run your JavaScript app for years on a coin cell - JSConf.asia 2016
Jan Jongboom
 
Solving connectivity for the Internet of Things - Telenor Group Technology Fair
Solving connectivity for the Internet of Things - Telenor Group Technology FairSolving connectivity for the Internet of Things - Telenor Group Technology Fair
Solving connectivity for the Internet of Things - Telenor Group Technology Fair
Jan Jongboom
 
How to prevent an Internet of Shit - AMSxTech 2017
How to prevent an Internet of Shit - AMSxTech 2017How to prevent an Internet of Shit - AMSxTech 2017
How to prevent an Internet of Shit - AMSxTech 2017
Jan Jongboom
 

Viewers also liked (6)

Firefox OS Apps and Web APIs
Firefox OS Apps and Web APIsFirefox OS Apps and Web APIs
Firefox OS Apps and Web APIs
 
Firefox OS and the Internet of Things - NDC London 2014
Firefox OS and the Internet of Things - NDC London 2014Firefox OS and the Internet of Things - NDC London 2014
Firefox OS and the Internet of Things - NDC London 2014
 
Massive applications in node.js
Massive applications in node.jsMassive applications in node.js
Massive applications in node.js
 
Run your JavaScript app for years on a coin cell - JSConf.asia 2016
Run your JavaScript app for years on a coin cell - JSConf.asia 2016Run your JavaScript app for years on a coin cell - JSConf.asia 2016
Run your JavaScript app for years on a coin cell - JSConf.asia 2016
 
Solving connectivity for the Internet of Things - Telenor Group Technology Fair
Solving connectivity for the Internet of Things - Telenor Group Technology FairSolving connectivity for the Internet of Things - Telenor Group Technology Fair
Solving connectivity for the Internet of Things - Telenor Group Technology Fair
 
How to prevent an Internet of Shit - AMSxTech 2017
How to prevent an Internet of Shit - AMSxTech 2017How to prevent an Internet of Shit - AMSxTech 2017
How to prevent an Internet of Shit - AMSxTech 2017
 

Similar to ESNext for humans - LvivJS 16 August 2014

JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
JavaScript Proven Practises
JavaScript Proven PractisesJavaScript Proven Practises
JavaScript Proven PractisesRobert MacLean
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Всеволод Струкчинский: Node.js
Всеволод Струкчинский: Node.jsВсеволод Струкчинский: Node.js
Всеволод Струкчинский: Node.jsYandex
 
ES2015 New Features
ES2015 New FeaturesES2015 New Features
ES2015 New Features
Giacomo Zinetti
 
Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Introduction to ECMAScript 2015
Introduction to ECMAScript 2015
Tomasz Dziuda
 
Intro to Ember.JS 2016
Intro to Ember.JS 2016Intro to Ember.JS 2016
Intro to Ember.JS 2016
Sandino Núñez
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
Lukas Ruebbelke
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
Manoj Kumar
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
Iram Ramrajkar
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
Justin Alexander
 
Beyond java8
Beyond java8Beyond java8
Beyond java8
Muhammad Durrah
 
Sequelize
SequelizeSequelize
Sequelize
Tarek Raihan
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScript
kvangork
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascript
kvangork
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 

Similar to ESNext for humans - LvivJS 16 August 2014 (20)

JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
JavaScript Proven Practises
JavaScript Proven PractisesJavaScript Proven Practises
JavaScript Proven Practises
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
Всеволод Струкчинский: Node.js
Всеволод Струкчинский: Node.jsВсеволод Струкчинский: Node.js
Всеволод Струкчинский: Node.js
 
ES2015 New Features
ES2015 New FeaturesES2015 New Features
ES2015 New Features
 
Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Introduction to ECMAScript 2015
Introduction to ECMAScript 2015
 
Intro to Ember.JS 2016
Intro to Ember.JS 2016Intro to Ember.JS 2016
Intro to Ember.JS 2016
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Wakanday JS201 Best Practices
Wakanday JS201 Best PracticesWakanday JS201 Best Practices
Wakanday JS201 Best Practices
 
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
Beyond java8
Beyond java8Beyond java8
Beyond java8
 
Sequelize
SequelizeSequelize
Sequelize
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScript
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascript
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 

More from Jan Jongboom

TinyML on Arduino - workshop
TinyML on Arduino - workshopTinyML on Arduino - workshop
TinyML on Arduino - workshop
Jan Jongboom
 
Intelligent Edge - Getting started with TinyML for industrial applications
Intelligent Edge - Getting started with TinyML for industrial applicationsIntelligent Edge - Getting started with TinyML for industrial applications
Intelligent Edge - Getting started with TinyML for industrial applications
Jan Jongboom
 
Teaching your sensors new tricks with Machine Learning - Eta Compute webinar
Teaching your sensors new tricks with Machine Learning - Eta Compute webinarTeaching your sensors new tricks with Machine Learning - Eta Compute webinar
Teaching your sensors new tricks with Machine Learning - Eta Compute webinar
Jan Jongboom
 
Get started with TinyML - Embedded online conference
Get started with TinyML - Embedded online conferenceGet started with TinyML - Embedded online conference
Get started with TinyML - Embedded online conference
Jan Jongboom
 
Adding intelligence to your LoRaWAN deployment - The Things Virtual Conference
Adding intelligence to your LoRaWAN deployment - The Things Virtual ConferenceAdding intelligence to your LoRaWAN deployment - The Things Virtual Conference
Adding intelligence to your LoRaWAN deployment - The Things Virtual Conference
Jan Jongboom
 
Get started with TinyML - Hackster webinar 9 April 2020
Get started with TinyML - Hackster webinar 9 April 2020Get started with TinyML - Hackster webinar 9 April 2020
Get started with TinyML - Hackster webinar 9 April 2020
Jan Jongboom
 
Tiny intelligent computers and sensors - Open Hardware Event 2020
Tiny intelligent computers and sensors - Open Hardware Event 2020Tiny intelligent computers and sensors - Open Hardware Event 2020
Tiny intelligent computers and sensors - Open Hardware Event 2020
Jan Jongboom
 
Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019
Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019
Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019
Jan Jongboom
 
Adding intelligence to your LoRaWAN devices - The Things Conference on tour
Adding intelligence to your LoRaWAN devices - The Things Conference on tourAdding intelligence to your LoRaWAN devices - The Things Conference on tour
Adding intelligence to your LoRaWAN devices - The Things Conference on tour
Jan Jongboom
 
Machine learning on 1 square centimeter - Emerce Next 2019
Machine learning on 1 square centimeter - Emerce Next 2019Machine learning on 1 square centimeter - Emerce Next 2019
Machine learning on 1 square centimeter - Emerce Next 2019
Jan Jongboom
 
Fundamentals of IoT - Data Science Africa 2019
Fundamentals of IoT - Data Science Africa 2019Fundamentals of IoT - Data Science Africa 2019
Fundamentals of IoT - Data Science Africa 2019
Jan Jongboom
 
17,000 contributions in 32K RAM - FOSS North 2019
17,000 contributions in 32K RAM - FOSS North 201917,000 contributions in 32K RAM - FOSS North 2019
17,000 contributions in 32K RAM - FOSS North 2019
Jan Jongboom
 
Open Hours: Mbed Simulator
Open Hours: Mbed SimulatorOpen Hours: Mbed Simulator
Open Hours: Mbed Simulator
Jan Jongboom
 
Efficient IoT solutions based on LoRaWAN, The Things Network and Mbed OS
Efficient IoT solutions based on LoRaWAN, The Things Network and Mbed OSEfficient IoT solutions based on LoRaWAN, The Things Network and Mbed OS
Efficient IoT solutions based on LoRaWAN, The Things Network and Mbed OS
Jan Jongboom
 
Machine learning on 1 cm2 - Tweakers Dev Summit
Machine learning on 1 cm2 - Tweakers Dev SummitMachine learning on 1 cm2 - Tweakers Dev Summit
Machine learning on 1 cm2 - Tweakers Dev Summit
Jan Jongboom
 
Simulating LoRaWAN devices - LoRa Alliance AMM 2019
Simulating LoRaWAN devices - LoRa Alliance AMM 2019Simulating LoRaWAN devices - LoRa Alliance AMM 2019
Simulating LoRaWAN devices - LoRa Alliance AMM 2019
Jan Jongboom
 
Develop with Mbed OS - The Things Conference 2019
Develop with Mbed OS - The Things Conference 2019Develop with Mbed OS - The Things Conference 2019
Develop with Mbed OS - The Things Conference 2019
Jan Jongboom
 
Firmware Updates over LoRaWAN - The Things Conference 2019
Firmware Updates over LoRaWAN - The Things Conference 2019Firmware Updates over LoRaWAN - The Things Conference 2019
Firmware Updates over LoRaWAN - The Things Conference 2019
Jan Jongboom
 
Faster Device Development - GSMA @ CES 2019
Faster Device Development - GSMA @ CES 2019Faster Device Development - GSMA @ CES 2019
Faster Device Development - GSMA @ CES 2019
Jan Jongboom
 
Mbed LoRaWAN stack: a case study - LoRa Alliance AMM Tokyo
Mbed LoRaWAN stack: a case study - LoRa Alliance AMM TokyoMbed LoRaWAN stack: a case study - LoRa Alliance AMM Tokyo
Mbed LoRaWAN stack: a case study - LoRa Alliance AMM Tokyo
Jan Jongboom
 

More from Jan Jongboom (20)

TinyML on Arduino - workshop
TinyML on Arduino - workshopTinyML on Arduino - workshop
TinyML on Arduino - workshop
 
Intelligent Edge - Getting started with TinyML for industrial applications
Intelligent Edge - Getting started with TinyML for industrial applicationsIntelligent Edge - Getting started with TinyML for industrial applications
Intelligent Edge - Getting started with TinyML for industrial applications
 
Teaching your sensors new tricks with Machine Learning - Eta Compute webinar
Teaching your sensors new tricks with Machine Learning - Eta Compute webinarTeaching your sensors new tricks with Machine Learning - Eta Compute webinar
Teaching your sensors new tricks with Machine Learning - Eta Compute webinar
 
Get started with TinyML - Embedded online conference
Get started with TinyML - Embedded online conferenceGet started with TinyML - Embedded online conference
Get started with TinyML - Embedded online conference
 
Adding intelligence to your LoRaWAN deployment - The Things Virtual Conference
Adding intelligence to your LoRaWAN deployment - The Things Virtual ConferenceAdding intelligence to your LoRaWAN deployment - The Things Virtual Conference
Adding intelligence to your LoRaWAN deployment - The Things Virtual Conference
 
Get started with TinyML - Hackster webinar 9 April 2020
Get started with TinyML - Hackster webinar 9 April 2020Get started with TinyML - Hackster webinar 9 April 2020
Get started with TinyML - Hackster webinar 9 April 2020
 
Tiny intelligent computers and sensors - Open Hardware Event 2020
Tiny intelligent computers and sensors - Open Hardware Event 2020Tiny intelligent computers and sensors - Open Hardware Event 2020
Tiny intelligent computers and sensors - Open Hardware Event 2020
 
Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019
Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019
Teaching your sensors new tricks with Machine Learning - CENSIS Tech Summit 2019
 
Adding intelligence to your LoRaWAN devices - The Things Conference on tour
Adding intelligence to your LoRaWAN devices - The Things Conference on tourAdding intelligence to your LoRaWAN devices - The Things Conference on tour
Adding intelligence to your LoRaWAN devices - The Things Conference on tour
 
Machine learning on 1 square centimeter - Emerce Next 2019
Machine learning on 1 square centimeter - Emerce Next 2019Machine learning on 1 square centimeter - Emerce Next 2019
Machine learning on 1 square centimeter - Emerce Next 2019
 
Fundamentals of IoT - Data Science Africa 2019
Fundamentals of IoT - Data Science Africa 2019Fundamentals of IoT - Data Science Africa 2019
Fundamentals of IoT - Data Science Africa 2019
 
17,000 contributions in 32K RAM - FOSS North 2019
17,000 contributions in 32K RAM - FOSS North 201917,000 contributions in 32K RAM - FOSS North 2019
17,000 contributions in 32K RAM - FOSS North 2019
 
Open Hours: Mbed Simulator
Open Hours: Mbed SimulatorOpen Hours: Mbed Simulator
Open Hours: Mbed Simulator
 
Efficient IoT solutions based on LoRaWAN, The Things Network and Mbed OS
Efficient IoT solutions based on LoRaWAN, The Things Network and Mbed OSEfficient IoT solutions based on LoRaWAN, The Things Network and Mbed OS
Efficient IoT solutions based on LoRaWAN, The Things Network and Mbed OS
 
Machine learning on 1 cm2 - Tweakers Dev Summit
Machine learning on 1 cm2 - Tweakers Dev SummitMachine learning on 1 cm2 - Tweakers Dev Summit
Machine learning on 1 cm2 - Tweakers Dev Summit
 
Simulating LoRaWAN devices - LoRa Alliance AMM 2019
Simulating LoRaWAN devices - LoRa Alliance AMM 2019Simulating LoRaWAN devices - LoRa Alliance AMM 2019
Simulating LoRaWAN devices - LoRa Alliance AMM 2019
 
Develop with Mbed OS - The Things Conference 2019
Develop with Mbed OS - The Things Conference 2019Develop with Mbed OS - The Things Conference 2019
Develop with Mbed OS - The Things Conference 2019
 
Firmware Updates over LoRaWAN - The Things Conference 2019
Firmware Updates over LoRaWAN - The Things Conference 2019Firmware Updates over LoRaWAN - The Things Conference 2019
Firmware Updates over LoRaWAN - The Things Conference 2019
 
Faster Device Development - GSMA @ CES 2019
Faster Device Development - GSMA @ CES 2019Faster Device Development - GSMA @ CES 2019
Faster Device Development - GSMA @ CES 2019
 
Mbed LoRaWAN stack: a case study - LoRa Alliance AMM Tokyo
Mbed LoRaWAN stack: a case study - LoRa Alliance AMM TokyoMbed LoRaWAN stack: a case study - LoRa Alliance AMM Tokyo
Mbed LoRaWAN stack: a case study - LoRa Alliance AMM Tokyo
 

Recently uploaded

1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
VivekSinghShekhawat2
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
Javier Lasa
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 

Recently uploaded (20)

1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 

ESNext for humans - LvivJS 16 August 2014

  • 4.
  • 6.
  • 8.
  • 9.
  • 10. програма Things you could already do Things you needed a framework for Things that were impossible
  • 11. Things you can already do alt: S*#t that annoys Jan in JavaScript
  • 12. NodeList is not an array 1 document.querySelectorAll('li') 2 .filter(function(li) { 3 /* do something */ 4 }); 5 ERROR: document.querySelectorAll(...).filter is not a function
  • 13. NodeList is not an array 1 Array.prototype.slice.call( 2 document.querySelectorAll('li') 3 )
  • 14. NodeList is not an array 1 Array.prototype.slice.call( 2 document.querySelectorAll('li') 3 ) 1 Array.from( 2 document.querySelectorAll('li') 3 )
  • 15. Variable scoping 1 var a = 7; 2 3 if (something) { 4 var a = 9; 5 doSomethingElse(a); 6 } 7 8 console.log(a);
  • 16. Variable scoping 1 var a = 7; 2 3 if (something) { 4 var a = 9; 5 doSomethingElse(a); 6 } 7 8 console.log(a); // can be 7 or 9 depending on 'something'
  • 17. Variable scoping 1 var a = 7; 2 3 if (something) { 4 var a = 9; 5 doSomethingElse(a); 6 } 7 8 console.log(a); // can be 7 or 9 depending on 'something'
  • 18. Variable scoping 1 var a = 7; 2 3 if (something) { 4 var a = 9; 5 doSomethingElse(a); 6 } 7 8 console.log(a); // can be 7 or 9 depending on 'something'
  • 19. Variable scoping 1 let a = 7; 2 3 if (something) { 4 let a = 9; 5 doSomethingElse(a); 6 } 7 8 console.log(a); // always 7 ‘let’ to the rescue
  • 20. Variable scoping 1 let a = 7; 2 3 if (something) { 4 let a = 9; 5 doSomethingElse(a); 6 } 7 8 console.log(a); // always 7 ‘let’ to the rescue
  • 22. 1 function getPosition() { 2 return [ 12, 91 ]; 3 } 4 5 var pos = getPosition(); 6 var x = pos[0], 7 y = pos[1]; 8 9 function getPosition() { 10 return { x: 12, y: 91 }; 11 } 12 13 var pos = getPosition(); 14 var x = pos.x; 15 y = pos.y;
  • 23. 1 function getPosition() { 2 return [ 12, 91 ]; 3 } 4 5 var pos = getPosition(); 6 var x = pos[0], 7 y = pos[1]; 8 9 function getPosition() { 10 return { x: 12, y: 91 }; 11 } 12 13 var pos = getPosition(); 14 var x = pos.x; 15 y = pos.y; let [x, y] = getPosition();
  • 24. 1 function getPosition() { 2 return [ 12, 91 ]; 3 } 4 5 var pos = getPosition(); 6 var x = pos[0], 7 y = pos[1]; 8 9 function getPosition() { 10 return { x: 12, y: 91 }; 11 } 12 13 var pos = getPosition(); 14 var x = pos.x; 15 y = pos.y; let [x, y] = getPosition(); let {x, y} = getPosition();
  • 25. 1 var data = {}; 2 3 data['jan'] = 'awesome'; 4 data['pete'] = 'not so awesome'; 5 6 var ele1 = document.querySelector('#el1'); 7 data[ele1] = 'first element'; 8 9 var ele2 = document.querySelector('#el2'); 10 data[ele2] = 'second element'; 11 12 console.log(data[ele1]); Maps
  • 26. 1 var data = {}; 2 3 data['jan'] = 'awesome'; 4 data['pete'] = 'not so awesome'; 5 6 var ele1 = document.querySelector('#el1'); 7 data[ele1] = 'first element'; 8 9 var ele2 = document.querySelector('#el2'); 10 data[ele2] = 'second element'; 11 12 console.log(data[ele1]); Maps
  • 27. 1 var data = {}; 2 3 data['jan'] = 'awesome'; 4 data['pete'] = 'not so awesome'; 5 6 var ele1 = document.querySelector('#el1'); 7 data[ele1] = 'first element'; 8 9 var ele2 = document.querySelector('#el2'); 10 data[ele2] = 'second element'; 11 12 console.log(data[ele1]); Maps “second element”
  • 28. 1 var data = {}; 2 3 data['jan'] = 'awesome'; 4 data['pete'] = 'not so awesome'; 5 6 var ele1 = document.querySelector('#el1'); 7 data[ele1] = 'first element'; 8 9 var ele2 = document.querySelector('#el2'); 10 data[ele2] = 'second element'; 11 12 console.log(data[ele1]); Maps
  • 29. Maps 1 var data = {}; 2 3 data['jan'] = 'awesome'; 4 data['pete'] = 'not so awesome'; 5 6 var ele1 = document.querySelector('#el1'); 7 data[ele1] = 'first element'; 8 9 var ele2 = document.querySelector('#el2'); 10 data[ele2] = 'second element'; 11 12 console.log(data[ele1]);
  • 30. Maps 1 var data = {}; 2 3 data['jan'] = 'awesome'; 4 data['pete'] = 'not so awesome'; 5 6 var ele1 = document.querySelector('#el1'); 7 data[ele1] = 'first element'; 8 9 var ele2 = document.querySelector('#el2'); 10 data[ele2] = 'second element'; 11 12 console.log(data[ele1]); data[ele2.toString()] ele2.toString() => “[object HTMLDivElement”]
  • 31. Maps 1 var data = new Map(); 2 3 data.set('jan', 'awesome'); 4 5 data.has('jan'); // true 6 data.get('jan'); // "awesome" 7 8 var ele1 = document.querySelector('#el1'); 9 data.set(ele1, 'first element'); 10 11 var ele2 = document.querySelector('#el2'); 12 data.set(ele2, 'second element'); 13 14 console.log(data.get(ele1)); // "first element"
  • 32. Maps 1 var data = new Map(); 2 3 data.set('jan', 'awesome'); 4 5 data.has('jan'); // true 6 data.get('jan'); // "awesome" 7 8 var ele1 = document.querySelector('#el1'); 9 data.set(ele1, 'first element'); 10 11 var ele2 = document.querySelector('#el2'); 12 data.set(ele2, 'second element'); 13 14 console.log(data.get(ele1)); // "first element"
  • 33. Function boilerplate 1 2 someArray 3 .filter(function(item) { 4 return item.isActive; 5 }) 6 .map(function(item) { 7 return item.id; 8 }); 9 10 setTimeout(function() { 11 doSomeMagic() 12 }, 500);
  • 34. Function boilerplate 1 2 someArray 3 .filter(function(item) { 4 return item.isActive; 5 }) 6 .map(function(item) { 7 return item.id; 8 }); 9 10 setTimeout(function() { 11 doSomeMagic() 12 }, 500); .filter(item => item.isActive)
  • 35. Function boilerplate 1 2 someArray 3 .filter(function(item) { 4 return item.isActive; 5 }) 6 .map(function(item) { 7 return item.id; 8 }); 9 10 setTimeout(function() { 11 doSomeMagic() 12 }, 500); .filter(item => item.isActive) .map(item => item.id);
  • 36. Function boilerplate 1 2 someArray 3 .filter(function(item) { 4 return item.isActive; 5 }) 6 .map(function(item) { 7 return item.id; 8 }); 9 10 setTimeout(function() { 11 doSomeMagic() 12 }, 500); .filter(item => item.isActive) .map(item => item.id); setTimeout(() => {
  • 37.
  • 38. Things you need a framework for a.k.a. Goodbye Angular!
  • 40. 1 new Promise(function(resolve, reject) { 2 resolve('Success!'); 3 // or... 4 reject('I failed!'); 5 }); Native promises
  • 42. AngularJS 1 function SomeCtrl() { 2 $scope.name = "Jan"; 3 } 1 <div ng-controller="SomeCtrl"> 2 <input type="text" ng-model="name"> 3 4 Hi {{name}} 5 </div>
  • 44. From JS -> HTML 1 <div> 2 <input type="text" data-bind="name"> 3 Hello <span data-bind="name"></span> 4 </div> 1 var scope = { 2 name: 'Jan' 3 }; 4 // ???????
  • 45. From JS -> HTML 1 <div> 2 <input type="text" data-bind="name"> 3 Hello <span data-bind="name"></span> 4 </div> 1 var scope = { 2 name: 'Jan' 3 }; 4 // ???????
  • 46. From JS -> HTML 1 <div> 2 <input type="text" data-bind="name"> 3 Hello <span data-bind="name"></span> 4 </div> 1 var scope = { 2 name: 'Jan' 3 }; 4 // ???????
  • 47. 1 function updateScope() { 2 var els = document.querySelectorAll( 3 '*[data-bind=name]' 4 ); 5 6 Array.from(els).forEach(function(el) { 7 if ('value' in el) // input element 8 el.value = scope.name; 9 else if ('textContent' in el) // normal 10 el.textContent = scope.name; 11 }); 12 } 13 14 updateScope();
  • 48. 1 function updateScope() { 2 var els = document.querySelectorAll( 3 '*[data-bind=name]' 4 ); 5 6 Array.from(els).forEach(function(el) { 7 if ('value' in el) // input element 8 el.value = scope.name; 9 else if ('textContent' in el) // normal 10 el.textContent = scope.name; 11 }); 12 } 13 14 updateScope();
  • 49. 1 function updateScope() { 2 var els = document.querySelectorAll( 3 '*[data-bind=name]' 4 ); 5 6 Array.from(els).forEach(function(el) { 7 if ('value' in el) // input element 8 el.value = scope.name; 9 else if ('textContent' in el) // normal 10 el.textContent = scope.name; 11 }); 12 } 13 14 updateScope();
  • 50. 1 function updateScope() { 2 var els = document.querySelectorAll( 3 '*[data-bind=name]' 4 ); 5 6 Array.from(els).forEach(function(el) { 7 if ('value' in el) // input element 8 el.value = scope.name; 9 else if ('textContent' in el) // normal 10 el.textContent = scope.name; 11 }); 12 } 13 14 updateScope();
  • 51. 1 function updateScope() { 2 var els = document.querySelectorAll( 3 '*[data-bind=name]' 4 ); 5 6 Array.from(els).forEach(function(el) { 7 if ('value' in el) // input element 8 el.value = scope.name; 9 else if ('textContent' in el) // normal 10 el.textContent = scope.name; 11 }); 12 } 13 14 updateScope(); 1 <div> 2 <input type="text" data-bind="name"> 3 Hello <span data-bind="name"></span> 4 </div>
  • 52. But what if we change scope.name from JS?
  • 53. 1 Object.observe(scope, function(changes) { 2 changes.forEach(function(change) { 3 if (change.type === 'update' && 4 change.name === 'name') { 5 updateScope(); 6 } 7 }); 8 }); 9 10 11 setTimeout(function() { 12 scope.name = 'Vladimir'; 13 }, 2000);
  • 54. 1 Object.observe(scope, function(changes) { 2 changes.forEach(function(change) { 3 if (change.type === 'update' && 4 change.name === 'name') { 5 updateScope(); 6 } 7 }); 8 }); 9 10 11 setTimeout(function() { 12 scope.name = 'Vladimir'; 13 }, 2000);
  • 55. 1 Object.observe(scope, function(changes) { 2 changes.forEach(function(change) { 3 if (change.type === 'update' && 4 change.name === 'name') { 5 updateScope(); 6 } 7 }); 8 }); 9 10 11 setTimeout(function() { 12 scope.name = 'Vladimir'; 13 }, 2000); { type: "update", name: "name", oldValue: "Jan"}
  • 56. 1 Object.observe(scope, function(changes) { 2 changes.forEach(function(change) { 3 if (change.type === 'update' && 4 change.name === 'name') { 5 updateScope(); 6 } 7 }); 8 }); 9 10 11 setTimeout(function() { 12 scope.name = 'Vladimir'; 13 }, 2000); { type: "update", name: "name", oldValue: "Jan"}
  • 57.
  • 58. From HTML -> JS (1) 1 var els = document.querySelectorAll( 2 '*[data-bind=name]' 3 ); 4 5 var observer = new MutationObserver(function() { 6 scope.name = this.textContent; 7 }); 8 9 Array.from(els).forEach(function(el) { 10 observer.observe(el, { childList: true }); 11 });
  • 59. From HTML -> JS (1) 1 var els = document.querySelectorAll( 2 '*[data-bind=name]' 3 ); 4 5 var observer = new MutationObserver(function() { 6 scope.name = this.textContent; 7 }); 8 9 Array.from(els).forEach(function(el) { 10 observer.observe(el, { childList: true }); 11 });
  • 60.
  • 61. From HTML to JS (2) 1 var els = document.querySelectorAll( 2 '*[data-bind=name]' 3 ); 4 5 Array.from(els).forEach(function(el) { 6 el.onkeyup = function() { 7 if (el.value) 8 scope.name = el.value; 9 } 10 });
  • 62. From HTML to JS (2) 1 var els = document.querySelectorAll( 2 '*[data-bind=name]' 3 ); 4 5 Array.from(els).forEach(function(el) { 6 el.onkeyup = function() { 7 if (el.value) 8 scope.name = el.value; 9 } 10 });
  • 63.
  • 65. Things you cannot do a.k.a. I want it now!
  • 67. JS Object { id: 4, city: "Lviv" }
  • 68. JS Object { id: 4, city: "Lviv" } Application code
  • 69. JS Object { id: 4, city: "Lviv" } Application code alert(obj.id)
  • 70. JS Object { id: 4, city: "Lviv" } Application code alert(obj.id) obj.city = "Kiev"
  • 71. Proxy JS Object { id: 4, city: "Lviv" } Application code alert(obj.id) obj.city = "Kiev"
  • 72. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 console.log('Get for', name); 9 return target[name]; 10 } 11 } 12 ); 13 14 alert(obj.city); 15 // Get for 'city'
  • 73. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 console.log('Get for', name); 9 return target[name]; 10 } 11 } 12 ); 13 14 alert(obj.city); 15 // Get for 'city'
  • 74. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 console.log('Get for', name); 9 return target[name]; 10 } 11 } 12 ); 13 14 alert(obj.city); 15 // Get for 'city'
  • 75. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 console.log('Get for', name); 9 return target[name]; 10 } 11 } 12 ); 13 14 alert(obj.city); 15 // Get for 'city'
  • 76. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 if (name === 'city') { 9 return 'Kiev'; 10 } 11 return target[name]; 12 } 13 } 14 ); 15 16 console.log(obj.city); 17 // returns 'Kiev' ?!!
  • 77. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 if (name === 'city') { 9 return 'Kiev'; 10 } 11 return target[name]; 12 } 13 } 14 ); 15 16 console.log(obj.city); 17 // returns 'Kiev' ?!!
  • 78. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 if (!(name in target)) { 9 throw 'has no property "' + name + '"' 10 } 11 return target[name]; 12 } 13 } 14 ); 15 16 console.log(obj.citi); ERROR: has no property 'citi'
  • 79. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 get: function(target, name) { 8 if (!(name in target)) { 9 throw 'has no property "' + name + '"' 10 } 11 return target[name]; 12 } 13 } 14 ); 15 16 console.log(obj.citi); ERROR: has no property 'citi'
  • 80. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 set: function(target, name, value) { 8 if (name === 'id' 9 && typeof value !== 'number') 10 throw 'id must be numeric'; 11 target[name] = value; 12 } 13 } 14 ); 15 16 obj.id = '5'; ERROR: id must be numeric
  • 81. 1 var obj = new Proxy( 2 { 3 id: 4, 4 city: "Lviv" 5 }, 6 { 7 set: function(target, name, value) { 8 if (name === 'id' 9 && typeof value !== 'number') 10 throw 'id must be numeric'; 11 target[name] = value; 12 } 13 } 14 ); 15 16 obj.id = '5'; ERROR: id must be numeric
  • 82. 1 set: function(target, name, value) { 2 if (target[name] !== value) { 3 console.log('Value for', name, 'changed'); 4 } 5 }
  • 83. Proxies are cool Aspect Oriented Programming Logging Access Control
  • 84.
  • 87. Normal function 1 function normal() { 2 console.log('Hi there!'); 3 4 var a = 5 + 6; 5 console.log('I think it's', a); 6 7 console.log('kthxbye'); 8 9 return 3; 10 }
  • 88. Generators are lazy 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 turingWinners();
  • 89. Generators are lazy 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 turingWinners();
  • 90. Generators are lazy 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 turingWinners();
  • 91. Generators are lazy 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 turingWinners();
  • 92. Generators are lazy 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 turingWinners(); $ node --harmony test.js $
  • 93. Call next() to start 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 var iterator = turingWinners(); 11 12 console.log(iterator.next()); 13 console.log(iterator.next());
  • 94. Call next() to start 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 var iterator = turingWinners(); 11 12 console.log(iterator.next()); 13 console.log(iterator.next());
  • 95. Call next() to start 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 var iterator = turingWinners(); 11 12 console.log(iterator.next()); 13 console.log(iterator.next());
  • 96. Call next() to start 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 var iterator = turingWinners(); 11 12 console.log(iterator.next()); 13 console.log(iterator.next()); $ node --harmony test.js Hello from our function { value: 'Alan J. Perlis', done: false }
  • 97. Call next() to start 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 var iterator = turingWinners(); 11 12 console.log(iterator.next()); 13 console.log(iterator.next());
  • 98. Call next() to start 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 var iterator = turingWinners(); 11 12 console.log(iterator.next()); 13 console.log(iterator.next());
  • 99. Call next() to start 1 function* turingWinners () { 2 console.log('Hello from our function'); 3 yield "Alan J. Perlis"; 4 console.log('I returned the first one'); 5 yield "Maurice Wilkes"; 6 yield "Richard Hamming"; 7 yield "Marvin Minsky"; 8 } 9 10 var iterator = turingWinners(); 11 12 console.log(iterator.next()); 13 console.log(iterator.next()); $ node --harmony test.js Hello from our function { value: 'Alan J. Perlis', done: false } I returned the first one { value: 'Maurice Wilkes', done: false }
  • 100. Inef!cient thing in JS 1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12] 2 .filter(function(v) { 3 return v % 2 === 0; 4 }) 5 .map(function(v) { 6 return v * v; 7 }) 8 .slice(0, 3); 9 10 console.log(nice);
  • 101. Inef!cient thing in JS 1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12] 2 .filter(function(v) { 3 return v % 2 === 0; 4 }) 5 .map(function(v) { 6 return v * v; 7 }) 8 .slice(0, 3); 9 10 console.log(nice);
  • 102. Inef!cient thing in JS 1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12] 2 .filter(function(v) { 3 return v % 2 === 0; 4 }) 5 .map(function(v) { 6 return v * v; 7 }) 8 .slice(0, 3); 9 10 console.log(nice);
  • 103. Inef!cient thing in JS 1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12] 2 .filter(function(v) { 3 return v % 2 === 0; 4 }) 5 .map(function(v) { 6 return v * v; 7 }) 8 .slice(0, 3); 9 10 console.log(nice);
  • 104. Inef!cient thing in JS 1 var nice = [1,2,3,4,5,6,7,8,9,10,11,12] 2 .filter(function(v) { 3 return v % 2 === 0; 4 }) 5 .map(function(v) { 6 return v * v; 7 }) 8 .slice(0, 3); 9 10 console.log(nice);
  • 105. 1 function* generateNumber() { 2 var i = 0; 3 while (true) 4 yield ++i; 5 } 6 7 function* filter(it) { 8 for (var n of it) 9 if (n % 2 == 0) yield curr; 10 } 11 12 function* map(it) { 13 for (var n of it) 14 yield n * n; 15 }
  • 106. 1 function* generateNumber() { 2 var i = 0; 3 while (true) 4 yield ++i; 5 } 6 7 function* filter(it) { 8 for (var n of it) 9 if (n % 2 == 0) yield curr; 10 } 11 12 function* map(it) { 13 for (var n of it) 14 yield n * n; 15 }
  • 107. 1 function* generateNumber() { 2 var i = 0; 3 while (true) 4 yield ++i; 5 } 6 7 function* filter(it) { 8 for (var n of it) 9 if (n % 2 == 0) yield curr; 10 } 11 12 function* map(it) { 13 for (var n of it) 14 yield n * n; 15 } 1,2,3,4,5,6
  • 108. 1 function* generateNumber() { 2 var i = 0; 3 while (true) 4 yield ++i; 5 } 6 7 function* filter(it) { 8 for (var n of it) 9 if (n % 2 == 0) yield curr; 10 } 11 12 function* map(it) { 13 for (var n of it) 14 yield n * n; 15 } 1,2,3,4,5,6
  • 109. 1 function* generateNumber() { 2 var i = 0; 3 while (true) 4 yield ++i; 5 } 6 7 function* filter(it) { 8 for (var n of it) 9 if (n % 2 == 0) yield curr; 10 } 11 12 function* map(it) { 13 for (var n of it) 14 yield n * n; 15 } 1,2,3,4,5,6 n%2 == 0 2,4,6
  • 110. 1 function* generateNumber() { 2 var i = 0; 3 while (true) 4 yield ++i; 5 } 6 7 function* filter(it) { 8 for (var n of it) 9 if (n % 2 == 0) yield curr; 10 } 11 12 function* map(it) { 13 for (var n of it) 14 yield n * n; 15 } 1,2,3,4,5,6 n%2 == 0 2,4,6
  • 111. 1 function* generateNumber() { 2 var i = 0; 3 while (true) 4 yield ++i; 5 } 6 7 function* filter(it) { 8 for (var n of it) 9 if (n % 2 == 0) yield curr; 10 } 11 12 function* map(it) { 13 for (var n of it) 14 yield n * n; 15 } 1,2,3,4,5,6 n%2 == 0 2,4,6 n * n 4,16,36
  • 112. Using the generators 1 var nice = generateNumber(); 2 nice = filter(nice); 3 nice = map(nice); 4 5 for (var i = 0; i < 3; i++) { 6 console.log(i, nice.next().value); 7 }
  • 113. Using the generators 1 var nice = generateNumber(); 2 nice = filter(nice); 3 nice = map(nice); 4 5 for (var i = 0; i < 3; i++) { 6 console.log(i, nice.next().value); 7 }
  • 114.
  • 115. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000);
  • 116. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000);
  • 117. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000);
  • 118. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000);
  • 119. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000);
  • 120. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000);
  • 121. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000); $ node --harmony test.js It: Whats your name?
  • 122. Two way interaction 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 } 5 6 var iterator = printName(); 7 console.log('It:', iterator.next().value); 8 9 setTimeout(function() { 10 var ret = iterator.next('Jan Jongboom'); 11 console.log(ret); 12 }, 1000); $ node --harmony test.js It: Whats your name? $ node --harmony test.js It: Whats your name? Hello Jan Jongboom { value: undefined, done: true }
  • 123. Yield and deferred values • Wrote sync code • But yield waits until new value comes in... • So it’s actually async with sync syntax • We need to abuse this! 1 function* printName() { 2 var name = yield 'Whats your name?'; 3 console.log('Hello', name); 4 }
  • 124. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 125. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 126. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 127. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 128. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 129. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 130. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 131. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 132. 1 function* run() { 2 yield sleep(2000); 3 console.log('It has been 2 seconds'); 4 } 5 6 function sleep(ms) { 7 return new Promise((res, rej) => { 8 setTimeout(res, ms); 9 }); 10 } 11 12 var it = run(); 13 var ret = it.next().value; 14 15 ret.then(function() { 16 it.next(); 17 });
  • 133. http://pag.forbeslindesay.co.uk/#/22 1 run(function* () { 2 var data = yield $.get('/yolo'); 3 data = JSON.parse(data); 4 });
  • 134. http://pag.forbeslindesay.co.uk/#/22 1 run(function* () { 2 try { 3 var moar = yield $.post('/other'); 4 } 5 catch (ex) { 6 console.error('Oh noes!', ex); 7 } 8 });
  • 135. http://pag.forbeslindesay.co.uk/#/22 1 run(function* () { 2 var req1 = $.get('http://a'); 3 var req2 = $.get('http://b'); 4 5 $('.status').text( yield req1 + yield req2); 6 });
  • 137.
  • 138.