On Github tsherif / async-presentation
Tarek Sherif
$.get("http://my-url", function(res) { console.log(res); }); $("#clicker").click(function() { console.log("I was clicked!"); }); setTimeout(function() { console.log("It's time!"); }, 1000);
var x; setTimeout(function() { x = "Hello"; }, 1000); console.log(x);
var x; setTimeout(function() { x = "Hello"; }, 0); console.log(x);
// Take string and callback function. // Wait 0-500ms. // Call callback with string as argument. function async(string, callback) { setTimeout(function() { callback(string); }, Math.random() * 500); }
function printString(string) { console.log(string); } async("Print this first!", printString); async("Print this second!", printString);
function printString(string) { console.log(string); } async("Print this first!", function(string) { printString(string); async("Print this second!", printString); });
async("Why aren't we", function(string1) { async("running parallel?", function(string2) { var result = string1.length > string2.length ? string1 : string2; console.log(result); }); });
function combineResults(string) { // What goes here? } async("We are ", combineResults); async("running parallel!", combineResults);
var num_left = 2; var res = []; function combineResults(string) { res.push(string); if (--num_left === 0) { console.log(res[0].length > res[1].length ? res[0] : res[1]); } } async("We are ", combineResults); async("running parallel!", combineResults);
var num_left = 2; var strings = ["Keep us", "in order"]; var res = []; var i; for (i = 0; i < num_left; i++) { async(strings[i], function(string) { res[i] = string; if (--num_left === 0) { console.log(res.join(" ")); } }); }
var num_left = 2; var strings = ["Keep us", "in order"]; var res = []; var i; for (i = 0; i < num_left; i++) { (function(i){ async(strings[i], function(string) { res[i] = string; if (--num_left === 0) { console.log(res.join(" ")); } }); })(i); }
var worker = new Worker("url/of/worker.js"); worker.addEventListener("message", function(event) { console.log(event.data); }); worker.postMessage("Hello"); // ---- IN THE WORKER SCRIPT ---- self.addEventListener("message", function(event) { self.postMessage("Got message: " + event.data) });
async("Step 1", function(result) { console.log(result + " done!"); async("Step 2", function(result) { console.log(result + " done!"); async("Step 3", function(result) { console.log(result + " done!"); }); }); });
async("Step 1", doneStep1); function doneStep1(result) { console.log(result + " done!"); async("Step 2", doneStep2); } function doneStep2(result) { console.log(result + " done!"); async("Step 3", doneStep3); } function doneStep3(result) { console.log(result + " done!"); }
function get(url) { return new Promise(ajaxResolutionFunction); } get("http://first-url").then(function(result) { console.log(result); return get("http://next-url"); }).then(function(result) { console.log(result); return get("http://last-url"); }).then(function(result) { console.log(result); })
fancyAsync(function*(handleResult) { var res = yield async("It's as if ", handleResult); res += yield async("I'm running ", handleResult); res += yield async("synchronously!!!", handleResult); console.log(res); }); // Where the magic happens. function fancyAsync(generator) { var g = generator(handleResult); g.next(); function handleResult(response) { g.next(response); } }