SlideShare a Scribd company logo
1 of 84
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var result = load();
function load() {
var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
return data;
};
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function foo() {
setTimeout(function fn() {
result.concat([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}, 3000);
}
foo()
fn()
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var start = new Date();
setTimeout(function () {
var end = new Date();
console.log('Timeelapsed:', end - start, 'ms');
}, 500);
while (new Date() - start < 1000) { };
What will be the result:
1. 500 < result < 1000
2. 1000 < result < 1500
3. 1500 < result
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
This API does not guarantee that timers will run exactly on schedule.
Delays due to CPU load, other tasks, etc, are to be expected.
console.log("a");
setTimeout(function () { console.log("c"); }, 500);
setTimeout(function () { console.log("d"); }, 500);
setTimeout(function () { console.log("e"); }, 500);
console.log("b");
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var result = [];
load(result);
function load(result) {
setTimeout(function () {
result.concat([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}, 3000);
};
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var result;
load(function (data) {
result = data;
});
function load(callback) {
setTimeout(function () {
callback([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}, 3000);
};
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
init
function init() {
$("#spinner").show();
setup();
$("#spinner").hide();
}
setup
hide
show
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function init() {
$("#spinner").show();
setTimeout(
function() {
setup();
$("#spinner").hide();
}, 0);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
try {
setTimeout(function () {
throw new Error('Catch me if you can!');
}, 0);
} catch (e) {
console.error(e);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var worker = {
updateCustomer: function (customerInfo, callback ) { ... }
// other methods, properties, etc
};
worker.updateCustomer( currentCustomer, function (err, data) {
alert(err || data);
// this != worker
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
worker.updateCustomer(currentCustomer, function (err, data) {
this.showAlert(err || data);
}.bind(notifier));
// using underscore/lodash
worker.updateCustomer(currentCustomer, _.bind(function (err, data) {
this.showAlert(err || data);
}, notifier));
// using jquery
worker.updateCustomer(currentCustomer, $.proxy(function (err, data) {
this.showAlert(err || data);
}, notifier));
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
worker.updateCustomer(currentCustomer, function (err, data) {
this.showAlert(err || data);
}.bind(notifier));
// using underscore/lodash
worker.updateCustomer(currentCustomer, _.bind(function (err, data) {
this.showAlert(err || data);
}, notifier));
// using jquery
worker.updateCustomer(currentCustomer, $.proxy(function (err, data) {
this.showAlert(err || data);
}, notifier));
var updateForm = {
submit: function () {
// get the data and store it in currentCustomer
worker.updateCustomer(
currentCustomer, this.showAlert.bind(this) );
},
showAlert: function (err, data) {
// I don't care how, just show an alert :-)
}
};
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
for (var i = 0, len = items.length; i < len; i++) {
process(items[i]);
}
function processArray(items, process, callback) {
var todo = items.concat(); //create a clone of the original
setTimeout(function func() {
process(todo.shift());
if (todo.length > 0) {
setTimeout( func, 25 );
} else {
callback(items);
}
}, 25);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function saveDocument(id) {
//save the document
openDocument(id)
writeText(id);
closeDocument(id);
// update the UI to
// indicate success
updateUI(id);
}
function saveDocument(id) {
processArray(
[
openDocument,
writeText,
closeDocument,
updateUI
]
,function(item){ item(id)}
,function(){}
);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function timedProcessArray(items, process, callback) {
var todo = items.concat(); //create a clone of the original
setTimeout(function () {
var start = +new Date();
do {
process(todo.shift());
} while (todo.length > 0 && (+new Date() - start < 50));
if ( todo.length > 0 ) {
setTimeout( arguments.callee, 25 );
} else {
callback(items);
}
}, 25 );
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var allTheCustomerThings;
$("#getCustomer").click(function (cust) {
var id = $("#cust-id").val();
getCustomer(id, function (cust) {
allTheCustomerThings = cust;
getContacts(id, function (contacts) {
allTheCustomerThings.contacts = contacts;
getOrders(id, function (orders) {
allTheCustomerThings.orders = orders;
getAccountsRecv(id, function (ar) {
allTheCustomerThings.ar = ar;
// OK - we got all the data, NOW WHAT?! :-)
});
});
});
});
});
“The problem isn’t with the language itself;
it’s with the way programmers use the
language — Async Javascript.”
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var result;
load(function (data) {
result = data;
});
function load(callback) {
setTimeout(function () {
callback([1, 2, 3]);
}, 3000);
};
function load() {
return new Promise(
function (resolve,reject){
setTimeout(function(){
resolve([1, 2, 3]);
}, 3000);
});
}
var result;
var p = load();
p.then(function (data) {
result = data;
})
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var p1 = action()
var p2 = p1.then(fs,fe)
var p3 = p2.then(fs,fe) P1
P2
P3
fefs
fefs
fefs
action()
Resolve Reject
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
new Promise(function( resolve, reject) {...})
.then(onFulfilled, onRejected);
Promise
Return one of three:
1. undefined
2. value
3. Promise (fulfilled)
Fulfilled
Return one of two:
1. Exception
2. Promise (rejected)
Rejected
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
// fictional viewmodel for a mobile login screen
// the ugly nested callback version
var loginViewModel = (function () {
var login = function () {
var username = $('#loginUsername').val();
var password = $('#loginPassword').val();
el.Users.login(
username,
password,
function () {
usersModel.load(
function () {
mobileApp.navigate(
'views/notesView.html',
function () { // YAY! We made it! },
function (err) { showError(err.message); });
},
function (err) { showError(err.message); });
},
function (err) { showError(err.message); });
};
}());
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
// Converted to use promises
var loginViewModel = ( function () {
var login = function () {
var username = $('#loginUsername').val();
var password = $('#loginPassword').val();
el.Users.login(username, password)
.then(function () { return usersModel.load(); })
.then(function () { mobileApp.navigate('views/notesView.html');})
.then(
null, // YAY! We made it!
function (err) { showError(err.message); }
);
};
return { login: login };
})();
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function doAsync() {
var promise = doSomethingAsync();
promise.then(...);
return promise;
}
function doAsync() {
var promise = doSomethingAsync();
return promise.then(...);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
getJSON('story.json')
.then(function(story) {
return getJSON(story.chapterUrls[0]);
})
.then(function(chapter1) {
console.log("Got chapter 1", chapter1);
});
getJSON('story.json')
.then(function(story) {
getJSON(story.chapterUrls[0])
.then(function(chapter1) {
console.log("Got chapter 1", chapter1);
});
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
asyncThing1()
.then (function() { return asyncThing2(); })
.then (function() { return asyncThing3(); })
.catch(function(err) { return asyncRecovery1(); })
.then (function() { return asyncThing4(); },
function(err) { return asyncRecovery2(); })
.catch(function(err) { console.log("Don't worry about it"); })
.then (function() { console.log("All done!"); });
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
asyncThing1()
.then (function() { return asyncThing2(); })
.then (function() { return asyncThing3(); })
.catch(function(err) { return asyncRecovery1(); })
.then (function() { return asyncThing4(); },
function(err) { return asyncRecovery2(); })
.catch(function(err) { console.log("Don't worry about it");
})
.then (function() { console.log("All done!"); });
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var p1 = Promise.resolve(3);
var p2 = 1337;
var p3 = new Promise(function (resolve, reject) {
setTimeout(resolve, 100, "foo");
});
Promise.all([p1, p2, p3])
.then(function (values) {
console.log(values); // [3, 1337, "foo"]
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var p1 = new Promise(function (resolve, reject) {
setTimeout(resolve, 500, "one");
});
var p2 = new Promise(function (resolve, reject) {
setTimeout(resolve, 100, "two");
});
Promise.race([p1, p2]).then(function (value) {
console.log(value); // "two"
// Both resolve, but p2 is faster
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function timeout( promise , time ) {
return new Promise(function (fulfill, reject) {
// race promise against delay
promise.then(fulfill, reject);
delay(time).done(function () {
reject(new Error('Operation timed out'));
});
});
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function timeout(promise, time) {
return Promise.race(
[
promise,
delay(time)
.then(function () {
throw new Error('Operation timed out');
})
]);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
loadSomething().then(function(something) {
loadAnotherthing().then(function(another) {
DoSomethingOnThem(something, another);
});
});
q.all([ loadSomething() , loadAnotherThing() ])
.spread(function(something, another) {
DoSomethingOnThem(something, another);
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var allTheCustomerThings;
$("#getCustomer").click(function (cust) {
var id = $("#cust-id").val();
getCustomer(id, function (cust) {
allTheCustomerThings = cust;
getContacts(id, function (contacts) {
allTheCustomerThings.contacts = contacts;
getOrders(id, function (orders) {
allTheCustomerThings.orders = orders;
getAccountsRecv(id, function (ar) {
allTheCustomerThings.ar = ar;
// OK - we got all the data, NOW WHAT?! :-)
});
});
});
});
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
$("#getCustomer").click(function (cust) {
var id = $("#cust-id").val();
Q.spread([
getCustomer(id),
getContacts(id),
getOrders(id),
getAccountsRecv(id) ],
function (cust, contacts, orders, ar) {
cust.contacts = contacts;
cust.orders = orders;
cust.ar = ar;
}
);
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function processArray(items, process, callback) {
var todo = items.concat(); //create a clone of the original
setTimeout(function func() {
process(todo.shift());
if (todo.length > 0) {
setTimeout( func, 25 );
} else {
callback(items);
}
}, 25);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function processArray(items, process) {
return new Promise(function( resolve , reject ){
var todo = items.concat();
setTimeout(function func() {
process(todo.shift());
if (todo.length > 0) {
setTimeout( func, 25 );
} else {
resolve(items);
}
}, 25);
});
}
Process is
async?
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function processArray(items, process) {
return new Promise(function( resolve , reject ){
var todo = items.concat();
setTimeout(function func() {
process(todo.shift())
.then(function () {
if (todo.length > 0) {
setTimeout( func, 25 );
} else { resolve(items); }
});
}, 25);
});
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
// Option I
action();
.then(function(actionResult){});
// Option II
var result = await action();
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
// printDelayed is a 'Promise<void>'
async function printDelayed(elements: string[]) {
for (const element of elements) {
await delay(200);
console.log(element);
}
}
async function delay(milliseconds: number) {
return new Promise<void>(resolve => {
setTimeout(resolve, milliseconds);
});
}
printDelayed(["Hello", "beautiful", "world"]).then(() => {
console.log();
console.log("Printed every element!");
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var result;
load(function (data) {
result = data;
});
function load(callback) {
setTimeout(function () {
callback([1, 2, 3]);
}, 3000);
};
var result;
var p = load();
p.then(function (data) {
result = data;
})
function load() {
var defer = $.Defered();
setTimeout(function () {
defer.resolve([1, 2, 3]);
}, 3000);
return defer.promise();
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var obj = { hello: function (name) {alert(name); } },
defer = $.Deferred();
defer.promise(obj);
defer.resolve("John");
// Use the object as a Promise
obj.done(function (name) { obj.hello(name);})
.hello("Karl");
ListenToEvents (READ)
<< interface >>
Promise
ListenToEvents (READ)
TriggerEvents (WRITE)
<< interface >>
Deferred
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var serverData = {};
var getting1 = $.get('/1')
.done(function (result) { serverData['1'] = result; });
var getting2 = $.get('/2')
.done(function (result) { serverData['2'] = result; });
$.when(getting1, getting2)
.done(function () {
//the GET information is now in server Data...
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
$.ajax("/get/mail/"))
.done( newMessages,
updateMessageList,
updateUnreadIndicator )
.fail(noMessages)
.always(
function () {
var date = new Date();
$("#lastUpdated")
.html( date.toDateString() );
}
);
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var nanowrimoing = $.Deferred();
var wordGoal = 5000;
nanowrimoing.progress(function (wordCount) {
varpercentComplete = Math.floor(wordCount / wordGoal * 100);
$('#indicator').text(percentComplete + '%complete');
});
nanowrimoing.done(function () {
$('#indicator').text('Goodjob!');
});
$('#document').on('keypress', function () {
var wordCount = $(this).val().split(/s+/).length;
if (wordCount >= wordGoal) {
nanowrimoing.resolve();
};
nanowrimoing.notify(wordCount);
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var worker = new Worker('myThread.js');
worker.addEventListener('message',function(e){console.log(e.data);});
worker.postMessage('input message');
msg
//myThread.js
self.addEventListener( 'message' , doEcho );
function doEcho (e) { self.postMessage('Echo: ' + e.data) };
doEchoEcho
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
// Test if Dedicated Web Workers are available
if (window.Worker) { g_bDedicatedWorkersEnabled = true; }
// Test if Shared Web Workers are available
if (window.SharedWorker) { g_bSharedWorkersEnabled = true; }
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
worker.postMessage(
{data: int8View, moreData: anotherBuffer},
[int8View.buffer, anotherBuffer] );
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
importScripts('script1.js', 'script2.js');
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
 In Chrome, there's a nice page to view all of the created blob
URLs: chrome://blob-internals/
var blob = new Blob([
"onmessage = function(e) { postMessage('msg from worker'); }"]);
// Obtain a blob URL reference to our worker 'file'.
var blobURL = window.URL.createObjectURL(blob);
var worker = new Worker(blobURL);
worker.onmessage = function (e) { // e.data == 'msg from worker' };
worker.postMessage('msg'); // Start the worker
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function onError(e) {
document.getElementById('error').textContent = [
'ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message
].join('');
}
function onMsg(e) { ... }
var worker = new Worker('workerWithError.js');
worker.addEventListener('message', onMsg, false);
worker.addEventListener('error', onError, false);
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var calculator = operative({
add: function (a, b) {
return a + b;
}
});
// Calc on web worker and return the result to UI thread.
calculator.add(1, 2, function (result) {
result; // => 3
});
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Observable
Subscribe
Observer
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function action() {
return $http.get('/someUrl')
.then(function successCallback(response) { },
function errorCallback(response) { }
);
}
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function action() {
return http.get('/someUrl')
.map(res => res.data.json());
}
action()
.subscribe(onNextFn, onErrorFn, onComplateFn );
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
http://jaredforsyth.com/rxvision/
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
MSFT
27.01
INTC
21.75
MSFT
27.96
MSFT
31.21
INTC
22.54
INTC
20.98
MSFT
30.73
from tick in ticks
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
MSFT
27.01
INTC
21.75
MSFT
27.96
MSFT
31.21
INTC
22.54
INTC
20.98
MSFT
30.73
27.01 27.96 31.21 30.73
21.75 22.54 20.98
from tick in ticks
group tick by tick.Symbol
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
MSFT
27.01
INTC
21.75
MSFT
27.96
MSFT
31.21
INTC
22.54
INTC
20.98
MSFT
30.73
from tick in ticks
group tick by tick.Symbol into company
from openClose in company.Buffer(2, 1)
[27.01, 27.96] [27.96, 31.21] [31.21, 30.73]
[21.75, 22.54] [22.54, 20.98]
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
MSFT
27.01
INTC
21.75
MSFT
27.96
MSFT
31.21
INTC
22.54
INTC
20.98
MSFT
30.73
from tick in ticks
group tick by tick.Symbol into company
from openClose in company.Buffer(2, 1)
let diff = (openClose[1] – openClose[0]) / openClose[0]
0.034 0.104 -0.015
0.036 -0.069
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
MSFT
27.01
INTC
21.75
MSFT
27.96
MSFT
31.21
INTC
22.54
INTC
20.98
MSFT
30.73
from tick in ticks
group tick by tick.Symbol into company
from openClose in company.Buffer(2, 1)
let diff = (openClose[1] – openClose[0]) / openClose[0]
where diff > 0.1
0.034 0.104 -0.015
0.036 -0.069
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
MSFT
27.01
INTC
21.75
MSFT
27.96
MSFT
31.21
INTC
22.54
INTC
20.98
MSFT
30.73
from tick in ticks
group tick by tick.Symbol into company
from openClose in company.Buffer(2, 1)
let diff = (openClose[1] – openClose[0]) / openClose[0]
where diff > 0.1
select new { Company = company.Key, Increase = diff }
Company = MSFT
Increase = 0.104
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
 Autocomplete (source) (demo)
 Canvas Painting (source) (demo)
 Drag and Drop (source) (demo)
 AMD and Require.js Integration (source) (demo)
 Time Flies Like an Arrow (source) (demo)
Link to Start with: Introduction to the Rx for JavaScript
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Asynchronous JS: Callbacks, Listeners, Control Flow Libs
and Promises
Five Patterns to Help You Tame Asynchronous JavaScript
The basics of Web Workers
How JavaScript Timers Work
http://creativejs.com/resources/requestanimationframe/
© 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
eyalvardi.wordpress.com

More Related Content

What's hot

ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionDzmitry Ivashutsin
 
Specs Presentation
Specs PresentationSpecs Presentation
Specs PresentationSynesso
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wildJoe Morgan
 
Bootiful Development with Spring Boot and React
Bootiful Development with Spring Boot and ReactBootiful Development with Spring Boot and React
Bootiful Development with Spring Boot and ReactVMware Tanzu
 
My Top 5 APEX JavaScript API's
My Top 5 APEX JavaScript API'sMy Top 5 APEX JavaScript API's
My Top 5 APEX JavaScript API'sRoel Hartman
 
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Damien Carbery
 
ES6 Simplified
ES6 SimplifiedES6 Simplified
ES6 SimplifiedCarlos Ble
 
누구나 할 수 있다 Networking
누구나 할 수 있다 Networking누구나 할 수 있다 Networking
누구나 할 수 있다 NetworkingJungwon An
 
Why Redux-Observable?
Why Redux-Observable?Why Redux-Observable?
Why Redux-Observable?Anna Su
 
Python Yield
Python YieldPython Yield
Python Yieldyangjuven
 
Testing Services Effectively
Testing Services Effectively Testing Services Effectively
Testing Services Effectively Alberto Leal
 
Programação reativa e o actor model
Programação reativa e o actor modelProgramação reativa e o actor model
Programação reativa e o actor modelFabrício Rissetto
 
Two Trains and Other Refactoring Analogies
Two Trains and Other Refactoring AnalogiesTwo Trains and Other Refactoring Analogies
Two Trains and Other Refactoring AnalogiesKevin London
 
A gremlin in my graph confoo 2014
A gremlin in my graph confoo 2014A gremlin in my graph confoo 2014
A gremlin in my graph confoo 2014Damien Seguy
 
Node js overview
Node js overviewNode js overview
Node js overviewEyal Vardi
 
Android HttpClient - new slide!
Android HttpClient - new slide!Android HttpClient - new slide!
Android HttpClient - new slide!Chalermchon Samana
 

What's hot (18)

ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency Injection
 
Specs Presentation
Specs PresentationSpecs Presentation
Specs Presentation
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wild
 
Bootiful Development with Spring Boot and React
Bootiful Development with Spring Boot and ReactBootiful Development with Spring Boot and React
Bootiful Development with Spring Boot and React
 
My Top 5 APEX JavaScript API's
My Top 5 APEX JavaScript API'sMy Top 5 APEX JavaScript API's
My Top 5 APEX JavaScript API's
 
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
 
ES6 Simplified
ES6 SimplifiedES6 Simplified
ES6 Simplified
 
누구나 할 수 있다 Networking
누구나 할 수 있다 Networking누구나 할 수 있다 Networking
누구나 할 수 있다 Networking
 
Why Redux-Observable?
Why Redux-Observable?Why Redux-Observable?
Why Redux-Observable?
 
Python Yield
Python YieldPython Yield
Python Yield
 
Testing Services Effectively
Testing Services Effectively Testing Services Effectively
Testing Services Effectively
 
Programação reativa e o actor model
Programação reativa e o actor modelProgramação reativa e o actor model
Programação reativa e o actor model
 
#ajn3.lt.marblejenka
#ajn3.lt.marblejenka#ajn3.lt.marblejenka
#ajn3.lt.marblejenka
 
Two Trains and Other Refactoring Analogies
Two Trains and Other Refactoring AnalogiesTwo Trains and Other Refactoring Analogies
Two Trains and Other Refactoring Analogies
 
Programs
ProgramsPrograms
Programs
 
A gremlin in my graph confoo 2014
A gremlin in my graph confoo 2014A gremlin in my graph confoo 2014
A gremlin in my graph confoo 2014
 
Node js overview
Node js overviewNode js overview
Node js overview
 
Android HttpClient - new slide!
Android HttpClient - new slide!Android HttpClient - new slide!
Android HttpClient - new slide!
 

Viewers also liked

Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 PipesEyal Vardi
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationEyal Vardi
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0Eyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0Eyal Vardi
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Eyal Vardi
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injectionEyal Vardi
 
Node.js File system & Streams
Node.js File system & StreamsNode.js File system & Streams
Node.js File system & StreamsEyal Vardi
 
Node.js Socket.IO
Node.js  Socket.IONode.js  Socket.IO
Node.js Socket.IOEyal Vardi
 
Node.js Event Emitter
Node.js Event EmitterNode.js Event Emitter
Node.js Event EmitterEyal Vardi
 
Modules and injector
Modules and injectorModules and injector
Modules and injectorEyal Vardi
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 ViewsEyal Vardi
 
Node.js Spplication Scaling
Node.js Spplication ScalingNode.js Spplication Scaling
Node.js Spplication ScalingEyal Vardi
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And NavigationEyal Vardi
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModuleEyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xEyal Vardi
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS OverviewEyal Vardi
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationEyal Vardi
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2Eyal Vardi
 

Viewers also liked (20)

Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 Pipes
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
Node.js File system & Streams
Node.js File system & StreamsNode.js File system & Streams
Node.js File system & Streams
 
Node.js Socket.IO
Node.js  Socket.IONode.js  Socket.IO
Node.js Socket.IO
 
Node.js Event Emitter
Node.js Event EmitterNode.js Event Emitter
Node.js Event Emitter
 
Modules and injector
Modules and injectorModules and injector
Modules and injector
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 Views
 
Nodejs
NodejsNodejs
Nodejs
 
Node.js Spplication Scaling
Node.js Spplication ScalingNode.js Spplication Scaling
Node.js Spplication Scaling
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModule
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS Overview
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 

Similar to Async & Parallel in JavaScript

What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0Eyal Vardi
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScriptEyal Vardi
 
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST ServicesEWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST ServicesRob Tweed
 
GlueCon 2016 - Threading in JavaScript
GlueCon 2016 - Threading in JavaScriptGlueCon 2016 - Threading in JavaScript
GlueCon 2016 - Threading in JavaScriptJonathan Baker
 
OOP in JavaScript
OOP in JavaScriptOOP in JavaScript
OOP in JavaScriptEyal Vardi
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014Guillaume POTIER
 
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!Guilherme Carreiro
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ EtsyNishan Subedi
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer ToolboxAdding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer ToolboxJeff Strauss
 
Forms in AngularJS
Forms in AngularJSForms in AngularJS
Forms in AngularJSEyal Vardi
 
R57shell
R57shellR57shell
R57shellady36
 
AMD & Require.js
AMD & Require.jsAMD & Require.js
AMD & Require.jsEyal Vardi
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your CodeAbbas Ali
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014alexandre freire
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Arc & Codementor
 
What's new in ECMAScript 6
What's new in ECMAScript 6What's new in ECMAScript 6
What's new in ECMAScript 6Ran Wahle
 
EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...
EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...
EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...Rob Tweed
 
More than syntax
More than syntaxMore than syntax
More than syntaxWooga
 

Similar to Async & Parallel in JavaScript (20)

What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScript
 
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST ServicesEWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
 
ES2015 New Features
ES2015 New FeaturesES2015 New Features
ES2015 New Features
 
GlueCon 2016 - Threading in JavaScript
GlueCon 2016 - Threading in JavaScriptGlueCon 2016 - Threading in JavaScript
GlueCon 2016 - Threading in JavaScript
 
OOP in JavaScript
OOP in JavaScriptOOP in JavaScript
OOP in JavaScript
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014
 
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer ToolboxAdding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
 
Forms in AngularJS
Forms in AngularJSForms in AngularJS
Forms in AngularJS
 
R57shell
R57shellR57shell
R57shell
 
AMD & Require.js
AMD & Require.jsAMD & Require.js
AMD & Require.js
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
 
What's new in ECMAScript 6
What's new in ECMAScript 6What's new in ECMAScript 6
What's new in ECMAScript 6
 
EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...
EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...
EWD 3 Training Course Part 13: Putting Everything so far into Practice using ...
 
More than syntax
More than syntaxMore than syntax
More than syntax
 

More from Eyal Vardi

Smart Contract
Smart ContractSmart Contract
Smart ContractEyal Vardi
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipesEyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Eyal Vardi
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 ArchitectureEyal Vardi
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xEyal Vardi
 
Symbols in ECMAScript 6.0
Symbols in ECMAScript 6.0Symbols in ECMAScript 6.0
Symbols in ECMAScript 6.0Eyal Vardi
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS InternalEyal Vardi
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 

More from Eyal Vardi (9)

Why magic
Why magicWhy magic
Why magic
 
Smart Contract
Smart ContractSmart Contract
Smart Contract
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipes
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 Architecture
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.x
 
Symbols in ECMAScript 6.0
Symbols in ECMAScript 6.0Symbols in ECMAScript 6.0
Symbols in ECMAScript 6.0
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 

Recently uploaded

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

Async & Parallel in JavaScript

  • 1. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 2. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 3. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 4. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var result = load(); function load() { var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; return data; };
  • 5. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function foo() { setTimeout(function fn() { result.concat([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }, 3000); } foo() fn()
  • 6. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var start = new Date(); setTimeout(function () { var end = new Date(); console.log('Timeelapsed:', end - start, 'ms'); }, 500); while (new Date() - start < 1000) { }; What will be the result: 1. 500 < result < 1000 2. 1000 < result < 1500 3. 1500 < result
  • 7. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com This API does not guarantee that timers will run exactly on schedule. Delays due to CPU load, other tasks, etc, are to be expected. console.log("a"); setTimeout(function () { console.log("c"); }, 500); setTimeout(function () { console.log("d"); }, 500); setTimeout(function () { console.log("e"); }, 500); console.log("b");
  • 8. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var result = []; load(result); function load(result) { setTimeout(function () { result.concat([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }, 3000); };
  • 9. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var result; load(function (data) { result = data; }); function load(callback) { setTimeout(function () { callback([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }, 3000); };
  • 10. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 11. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com init function init() { $("#spinner").show(); setup(); $("#spinner").hide(); } setup hide show
  • 12. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function init() { $("#spinner").show(); setTimeout( function() { setup(); $("#spinner").hide(); }, 0); }
  • 13. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com try { setTimeout(function () { throw new Error('Catch me if you can!'); }, 0); } catch (e) { console.error(e); }
  • 14. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var worker = { updateCustomer: function (customerInfo, callback ) { ... } // other methods, properties, etc }; worker.updateCustomer( currentCustomer, function (err, data) { alert(err || data); // this != worker });
  • 15. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com worker.updateCustomer(currentCustomer, function (err, data) { this.showAlert(err || data); }.bind(notifier)); // using underscore/lodash worker.updateCustomer(currentCustomer, _.bind(function (err, data) { this.showAlert(err || data); }, notifier)); // using jquery worker.updateCustomer(currentCustomer, $.proxy(function (err, data) { this.showAlert(err || data); }, notifier));
  • 16. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com worker.updateCustomer(currentCustomer, function (err, data) { this.showAlert(err || data); }.bind(notifier)); // using underscore/lodash worker.updateCustomer(currentCustomer, _.bind(function (err, data) { this.showAlert(err || data); }, notifier)); // using jquery worker.updateCustomer(currentCustomer, $.proxy(function (err, data) { this.showAlert(err || data); }, notifier)); var updateForm = { submit: function () { // get the data and store it in currentCustomer worker.updateCustomer( currentCustomer, this.showAlert.bind(this) ); }, showAlert: function (err, data) { // I don't care how, just show an alert :-) } };
  • 17. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com for (var i = 0, len = items.length; i < len; i++) { process(items[i]); } function processArray(items, process, callback) { var todo = items.concat(); //create a clone of the original setTimeout(function func() { process(todo.shift()); if (todo.length > 0) { setTimeout( func, 25 ); } else { callback(items); } }, 25); }
  • 18. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function saveDocument(id) { //save the document openDocument(id) writeText(id); closeDocument(id); // update the UI to // indicate success updateUI(id); } function saveDocument(id) { processArray( [ openDocument, writeText, closeDocument, updateUI ] ,function(item){ item(id)} ,function(){} ); }
  • 19. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function timedProcessArray(items, process, callback) { var todo = items.concat(); //create a clone of the original setTimeout(function () { var start = +new Date(); do { process(todo.shift()); } while (todo.length > 0 && (+new Date() - start < 50)); if ( todo.length > 0 ) { setTimeout( arguments.callee, 25 ); } else { callback(items); } }, 25 ); }
  • 20. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 21. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 22. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var allTheCustomerThings; $("#getCustomer").click(function (cust) { var id = $("#cust-id").val(); getCustomer(id, function (cust) { allTheCustomerThings = cust; getContacts(id, function (contacts) { allTheCustomerThings.contacts = contacts; getOrders(id, function (orders) { allTheCustomerThings.orders = orders; getAccountsRecv(id, function (ar) { allTheCustomerThings.ar = ar; // OK - we got all the data, NOW WHAT?! :-) }); }); }); }); }); “The problem isn’t with the language itself; it’s with the way programmers use the language — Async Javascript.”
  • 23. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var result; load(function (data) { result = data; }); function load(callback) { setTimeout(function () { callback([1, 2, 3]); }, 3000); }; function load() { return new Promise( function (resolve,reject){ setTimeout(function(){ resolve([1, 2, 3]); }, 3000); }); } var result; var p = load(); p.then(function (data) { result = data; })
  • 24. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 25. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 26. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var p1 = action() var p2 = p1.then(fs,fe) var p3 = p2.then(fs,fe) P1 P2 P3 fefs fefs fefs action() Resolve Reject
  • 27. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com new Promise(function( resolve, reject) {...}) .then(onFulfilled, onRejected); Promise Return one of three: 1. undefined 2. value 3. Promise (fulfilled) Fulfilled Return one of two: 1. Exception 2. Promise (rejected) Rejected
  • 28. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // fictional viewmodel for a mobile login screen // the ugly nested callback version var loginViewModel = (function () { var login = function () { var username = $('#loginUsername').val(); var password = $('#loginPassword').val(); el.Users.login( username, password, function () { usersModel.load( function () { mobileApp.navigate( 'views/notesView.html', function () { // YAY! We made it! }, function (err) { showError(err.message); }); }, function (err) { showError(err.message); }); }, function (err) { showError(err.message); }); }; }());
  • 29. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // Converted to use promises var loginViewModel = ( function () { var login = function () { var username = $('#loginUsername').val(); var password = $('#loginPassword').val(); el.Users.login(username, password) .then(function () { return usersModel.load(); }) .then(function () { mobileApp.navigate('views/notesView.html');}) .then( null, // YAY! We made it! function (err) { showError(err.message); } ); }; return { login: login }; })();
  • 30. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function doAsync() { var promise = doSomethingAsync(); promise.then(...); return promise; } function doAsync() { var promise = doSomethingAsync(); return promise.then(...); }
  • 31. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com getJSON('story.json') .then(function(story) { return getJSON(story.chapterUrls[0]); }) .then(function(chapter1) { console.log("Got chapter 1", chapter1); }); getJSON('story.json') .then(function(story) { getJSON(story.chapterUrls[0]) .then(function(chapter1) { console.log("Got chapter 1", chapter1); }); });
  • 32. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com asyncThing1() .then (function() { return asyncThing2(); }) .then (function() { return asyncThing3(); }) .catch(function(err) { return asyncRecovery1(); }) .then (function() { return asyncThing4(); }, function(err) { return asyncRecovery2(); }) .catch(function(err) { console.log("Don't worry about it"); }) .then (function() { console.log("All done!"); });
  • 33. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com asyncThing1() .then (function() { return asyncThing2(); }) .then (function() { return asyncThing3(); }) .catch(function(err) { return asyncRecovery1(); }) .then (function() { return asyncThing4(); }, function(err) { return asyncRecovery2(); }) .catch(function(err) { console.log("Don't worry about it"); }) .then (function() { console.log("All done!"); });
  • 34. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var p1 = Promise.resolve(3); var p2 = 1337; var p3 = new Promise(function (resolve, reject) { setTimeout(resolve, 100, "foo"); }); Promise.all([p1, p2, p3]) .then(function (values) { console.log(values); // [3, 1337, "foo"] });
  • 35. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var p1 = new Promise(function (resolve, reject) { setTimeout(resolve, 500, "one"); }); var p2 = new Promise(function (resolve, reject) { setTimeout(resolve, 100, "two"); }); Promise.race([p1, p2]).then(function (value) { console.log(value); // "two" // Both resolve, but p2 is faster });
  • 36. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function delay(time) { return new Promise(function (resolve) { setTimeout(resolve, time); }); }
  • 37. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function timeout( promise , time ) { return new Promise(function (fulfill, reject) { // race promise against delay promise.then(fulfill, reject); delay(time).done(function () { reject(new Error('Operation timed out')); }); }); }
  • 38. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function timeout(promise, time) { return Promise.race( [ promise, delay(time) .then(function () { throw new Error('Operation timed out'); }) ]); }
  • 39. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com loadSomething().then(function(something) { loadAnotherthing().then(function(another) { DoSomethingOnThem(something, another); }); }); q.all([ loadSomething() , loadAnotherThing() ]) .spread(function(something, another) { DoSomethingOnThem(something, another); });
  • 40. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 41. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var allTheCustomerThings; $("#getCustomer").click(function (cust) { var id = $("#cust-id").val(); getCustomer(id, function (cust) { allTheCustomerThings = cust; getContacts(id, function (contacts) { allTheCustomerThings.contacts = contacts; getOrders(id, function (orders) { allTheCustomerThings.orders = orders; getAccountsRecv(id, function (ar) { allTheCustomerThings.ar = ar; // OK - we got all the data, NOW WHAT?! :-) }); }); }); }); });
  • 42. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com $("#getCustomer").click(function (cust) { var id = $("#cust-id").val(); Q.spread([ getCustomer(id), getContacts(id), getOrders(id), getAccountsRecv(id) ], function (cust, contacts, orders, ar) { cust.contacts = contacts; cust.orders = orders; cust.ar = ar; } ); });
  • 43. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function processArray(items, process, callback) { var todo = items.concat(); //create a clone of the original setTimeout(function func() { process(todo.shift()); if (todo.length > 0) { setTimeout( func, 25 ); } else { callback(items); } }, 25); }
  • 44. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function processArray(items, process) { return new Promise(function( resolve , reject ){ var todo = items.concat(); setTimeout(function func() { process(todo.shift()); if (todo.length > 0) { setTimeout( func, 25 ); } else { resolve(items); } }, 25); }); } Process is async?
  • 45. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function processArray(items, process) { return new Promise(function( resolve , reject ){ var todo = items.concat(); setTimeout(function func() { process(todo.shift()) .then(function () { if (todo.length > 0) { setTimeout( func, 25 ); } else { resolve(items); } }); }, 25); }); }
  • 46. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // Option I action(); .then(function(actionResult){}); // Option II var result = await action();
  • 47. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // printDelayed is a 'Promise<void>' async function printDelayed(elements: string[]) { for (const element of elements) { await delay(200); console.log(element); } } async function delay(milliseconds: number) { return new Promise<void>(resolve => { setTimeout(resolve, milliseconds); }); } printDelayed(["Hello", "beautiful", "world"]).then(() => { console.log(); console.log("Printed every element!"); });
  • 48. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 49. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 50. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var result; load(function (data) { result = data; }); function load(callback) { setTimeout(function () { callback([1, 2, 3]); }, 3000); }; var result; var p = load(); p.then(function (data) { result = data; }) function load() { var defer = $.Defered(); setTimeout(function () { defer.resolve([1, 2, 3]); }, 3000); return defer.promise(); }
  • 51. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var obj = { hello: function (name) {alert(name); } }, defer = $.Deferred(); defer.promise(obj); defer.resolve("John"); // Use the object as a Promise obj.done(function (name) { obj.hello(name);}) .hello("Karl"); ListenToEvents (READ) << interface >> Promise ListenToEvents (READ) TriggerEvents (WRITE) << interface >> Deferred
  • 52. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var serverData = {}; var getting1 = $.get('/1') .done(function (result) { serverData['1'] = result; }); var getting2 = $.get('/2') .done(function (result) { serverData['2'] = result; }); $.when(getting1, getting2) .done(function () { //the GET information is now in server Data... });
  • 53. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com $.ajax("/get/mail/")) .done( newMessages, updateMessageList, updateUnreadIndicator ) .fail(noMessages) .always( function () { var date = new Date(); $("#lastUpdated") .html( date.toDateString() ); } );
  • 54. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var nanowrimoing = $.Deferred(); var wordGoal = 5000; nanowrimoing.progress(function (wordCount) { varpercentComplete = Math.floor(wordCount / wordGoal * 100); $('#indicator').text(percentComplete + '%complete'); }); nanowrimoing.done(function () { $('#indicator').text('Goodjob!'); }); $('#document').on('keypress', function () { var wordCount = $(this).val().split(/s+/).length; if (wordCount >= wordGoal) { nanowrimoing.resolve(); }; nanowrimoing.notify(wordCount); });
  • 55. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 56. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 57. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 58. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var worker = new Worker('myThread.js'); worker.addEventListener('message',function(e){console.log(e.data);}); worker.postMessage('input message'); msg //myThread.js self.addEventListener( 'message' , doEcho ); function doEcho (e) { self.postMessage('Echo: ' + e.data) }; doEchoEcho
  • 59. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // Test if Dedicated Web Workers are available if (window.Worker) { g_bDedicatedWorkersEnabled = true; } // Test if Shared Web Workers are available if (window.SharedWorker) { g_bSharedWorkersEnabled = true; }
  • 60. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 61. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 62. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com worker.postMessage( {data: int8View, moreData: anotherBuffer}, [int8View.buffer, anotherBuffer] );
  • 63. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com importScripts('script1.js', 'script2.js');
  • 64. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com  In Chrome, there's a nice page to view all of the created blob URLs: chrome://blob-internals/ var blob = new Blob([ "onmessage = function(e) { postMessage('msg from worker'); }"]); // Obtain a blob URL reference to our worker 'file'. var blobURL = window.URL.createObjectURL(blob); var worker = new Worker(blobURL); worker.onmessage = function (e) { // e.data == 'msg from worker' }; worker.postMessage('msg'); // Start the worker
  • 65. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function onError(e) { document.getElementById('error').textContent = [ 'ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message ].join(''); } function onMsg(e) { ... } var worker = new Worker('workerWithError.js'); worker.addEventListener('message', onMsg, false); worker.addEventListener('error', onError, false);
  • 66. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 67. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 68. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 69. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var calculator = operative({ add: function (a, b) { return a + b; } }); // Calc on web worker and return the result to UI thread. calculator.add(1, 2, function (result) { result; // => 3 });
  • 70. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 71. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 72. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com Observable Subscribe Observer
  • 73. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function action() { return $http.get('/someUrl') .then(function successCallback(response) { }, function errorCallback(response) { } ); }
  • 74. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function action() { return http.get('/someUrl') .map(res => res.data.json()); } action() .subscribe(onNextFn, onErrorFn, onComplateFn );
  • 75. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com http://jaredforsyth.com/rxvision/
  • 76. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com MSFT 27.01 INTC 21.75 MSFT 27.96 MSFT 31.21 INTC 22.54 INTC 20.98 MSFT 30.73 from tick in ticks
  • 77. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com MSFT 27.01 INTC 21.75 MSFT 27.96 MSFT 31.21 INTC 22.54 INTC 20.98 MSFT 30.73 27.01 27.96 31.21 30.73 21.75 22.54 20.98 from tick in ticks group tick by tick.Symbol
  • 78. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com MSFT 27.01 INTC 21.75 MSFT 27.96 MSFT 31.21 INTC 22.54 INTC 20.98 MSFT 30.73 from tick in ticks group tick by tick.Symbol into company from openClose in company.Buffer(2, 1) [27.01, 27.96] [27.96, 31.21] [31.21, 30.73] [21.75, 22.54] [22.54, 20.98]
  • 79. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com MSFT 27.01 INTC 21.75 MSFT 27.96 MSFT 31.21 INTC 22.54 INTC 20.98 MSFT 30.73 from tick in ticks group tick by tick.Symbol into company from openClose in company.Buffer(2, 1) let diff = (openClose[1] – openClose[0]) / openClose[0] 0.034 0.104 -0.015 0.036 -0.069
  • 80. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com MSFT 27.01 INTC 21.75 MSFT 27.96 MSFT 31.21 INTC 22.54 INTC 20.98 MSFT 30.73 from tick in ticks group tick by tick.Symbol into company from openClose in company.Buffer(2, 1) let diff = (openClose[1] – openClose[0]) / openClose[0] where diff > 0.1 0.034 0.104 -0.015 0.036 -0.069
  • 81. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com MSFT 27.01 INTC 21.75 MSFT 27.96 MSFT 31.21 INTC 22.54 INTC 20.98 MSFT 30.73 from tick in ticks group tick by tick.Symbol into company from openClose in company.Buffer(2, 1) let diff = (openClose[1] – openClose[0]) / openClose[0] where diff > 0.1 select new { Company = company.Key, Increase = diff } Company = MSFT Increase = 0.104
  • 82. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com  Autocomplete (source) (demo)  Canvas Painting (source) (demo)  Drag and Drop (source) (demo)  AMD and Require.js Integration (source) (demo)  Time Flies Like an Arrow (source) (demo) Link to Start with: Introduction to the Rx for JavaScript
  • 83. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises Five Patterns to Help You Tame Asynchronous JavaScript The basics of Web Workers How JavaScript Timers Work http://creativejs.com/resources/requestanimationframe/
  • 84. © 2016 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com eyalvardi.wordpress.com