This file contains 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
httpGet(url, once(processValue)); | |
function once(cb) { | |
var hasExecuted = false; | |
return function callbackWrapper() { | |
if (!hasExecuted) { | |
hasExecuted = true; | |
cb.apply(this, arguments); | |
} | |
} |
This file contains 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
httpGet(url1, function(res) { | |
// here we'll receive value1 | |
}); | |
httpGet(url2, function(res) { | |
// here we'll receive value2 | |
}); | |
function processValues(value) { | |
// we want to call this function first for value1, |
This file contains 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
// this doesn't work | |
try { | |
httpGet(function callback() { | |
// an error caused asynchronously | |
// cannot be caught via a callback | |
// mechanism | |
}) | |
} catch (err) { | |
// this will only get called if httpGet | |
// itself throws a synchronous error |
This file contains 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
// when performing an async operation that requires a callback | |
// we have to immediately provide that callback | |
// and would need to maintain or pass around a reference to | |
// the callback in our codebase if we wanted to handle the | |
// response in a different place from where we started the async | |
// process. | |
httpGet(function callback() { | |
// we have to provide this callback immediately | |
// when starting this operation and cannot do | |
// it at a later point. |
This file contains 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
var globalState; // callbacks have to talk through global state | |
httpGet(function callback1(value) { | |
globalState.callback1 = value; | |
}) | |
httpGet(function callback2(value) { | |
globalState.callback2 = value; | |
}) |
This file contains 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
[1,2,3].forEach(function callback(num) { | |
// this code runs synchronously | |
}) | |
// this line isn't reached until the | |
// array above is fully iterated on |
This file contains 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 * fibonacciGen() { | |
let prev1 = 1; | |
let prev2 = 1; | |
while (true) { | |
let next = prev1 + prev2; | |
yield prev1; | |
prev1 = prev2; | |
prev2 = next; | |
} | |
} |
This file contains 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
var fn = co.wrap(function* (val) { | |
// we can yield a promise | |
// which behaves like await-ing it | |
return yield Promise.resolve(val); | |
}); | |
fn(true).then(function (val) { | |
}); |
This file contains 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 computeMaxCallStackSize() { | |
try { | |
return 1 + computeMaxCallStackSize(); | |
} catch (e) { | |
return 1; | |
} | |
} |
This file contains 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 recurseForever() { | |
recurseForever(); | |
} | |
recurseForever(); | |
// outputs | |
// Uncaught RangeError: Maximum call stack size exceeded |