This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* fibonacci() { | |
var a = 0, b = 1, c = 0; | |
while (true) { | |
yield a; | |
c = a; | |
a = b; | |
b = c + b; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function(){ | |
_get("/something.ajax?greeting", function(err, greeting) { | |
if (err) { console.log(err); throw err; } | |
_get("/else.ajax?who&greeting="+greeting, function(err, who) { | |
if (err) { console.log(err); throw err; } | |
console.log(greeting+" "+who); | |
}); | |
}); | |
}, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sync(function* (resume) { | |
try (e) { | |
yield setTimeout(resume, 1000); | |
var greeting = yield _get('/something.ajax?greeting', resume) | |
var who = yield _get('/else.ajax?who&greeting=' + greeting, resume) | |
console.log(greeting + ' ' + who) | |
} | |
catch (e) { | |
console.log(e); | |
throw e; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function(){ | |
_get("/something.ajax?greeting", function(err, greeting) { | |
if (err) { console.log(err); throw err; } | |
_get("/else.ajax?who&greeting="+greeting, function(err, who) { | |
if (err) { console.log(err); throw err; } | |
console.log(greeting+" "+who); | |
}); | |
}); | |
}, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function procrastinatingAdd(x, y) { | |
var errMsg = "Expected number and got "; | |
setTimeout(function() { | |
if (isNaN(x)) gen.throw(new TypeError(errMsg + typeof x)); | |
if (isNaN(y)) gen.throw(new TypeError(errMsg + typeof y)); | |
gen.next(x + y); | |
}, 500); | |
} | |
var gen = function* () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in the test code: | |
it("updates the page title on click", function() { | |
assert.equals(view.find("h1").text(), "before update"); | |
view.find("button").trigger("click"); // uh oh-async! | |
assert.equals(view.find("h1").text(), "after update"); // will fail due to callback being part of different stack | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function changeHeaderDeferred() { | |
var header = document.getElementById("header"); | |
setTimeout(function changeHeader() { | |
header.style.color = "red"; | |
return false; | |
}, 100); | |
return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// our main code, in a <script>-tag in our HTML page | |
var piWorker = new Worker("pi_calculator.js"); | |
var logResult = function(e) { | |
console.log("PI: " + e.data); | |
}; | |
piWorker.addEventListener("message", logResult, false); | |
piWorker.postMessage(100000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// our worker, which does some CPU-intensive operation | |
var reportResult = function(e) { | |
pi = SomeLib.computePiToSpecifiedDecimals(e.data); | |
postMessage(pi); | |
}; | |
onmessage = reportResult; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function f() { | |
console.log("foo"); | |
setTimeout(g, 0); | |
console.log("baz"); | |
h(); | |
} | |
function g() { | |
console.log("bar"); | |
} |