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
getUserData(function doStuff(e, a) { | |
getMoreUserData(function doMoreStuff(e, b) { | |
getEvenMoreUserData(function doEvenMoreStuff(e, c) { | |
getYetMoreUserData(function doYetMoreStuff(e, c) { | |
console.log('Welcome to callback hell!'); | |
}); | |
}); | |
}); | |
}) |
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
// Suppose that we have a method fetchJson() that does GET requests and has an interface that looks | |
// like this, where callback is expected to take error as its first argument and the parsed response | |
// data as its second. | |
function fetchJson(url, callback) { ... } | |
fetchJson('/api/user/self', function(e, user) { | |
fetchJson('/api/interests?userId=' + user.id, function(e, interests) { | |
var recommendations = []; | |
interests.forEach(function () { | |
fetchJson('/api/recommendations?topic=' + interest, function(e, recommendation) { | |
recommendations.push(recommendation); |
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
fetchJson('/api/user/self') | |
.then(function (user) { | |
return fetchJson('/api/user/interests?userId=' + self.id); | |
}) | |
.then(function (interests) { | |
return Promise.all[interests.map(i => fetchJson('/api/recommendations?topic=' + i))]; | |
}) | |
.then(function (recommendations) { | |
render(user, interests, recommendations); | |
}); |
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
fetchJson('/api/user/self') | |
.then(function (user) { | |
return fetchJson('/api/user/interests?userId=' + self.id) | |
.then(interests => { | |
user: user, | |
interests: interests | |
}); | |
}) | |
.then(function (blob) { | |
return Promise.all[blob.interests.map(i => fetchJson('/api/recommendations?topic=' + i))] |
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
co(function* () { | |
var user = yield fetchJson('/api/user/self'); | |
var interests = yield fetchJson('/api/user/interests?userId=' + self.id); | |
var recommendations = yield Promise.all( | |
interests.map(i => fetchJson('/api/recommendations?topic=' + i))); | |
render(user, interests, recommendations); | |
}); |
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* counts(start) { | |
yield start + 1; | |
yield start + 2; | |
yield start + 3; | |
return start + 4; | |
} | |
const counter = counts(0); | |
console.log(counter.next()); // {value: 1, done: false} | |
console.log(counter.next()); // {value: 2, done: false} | |
console.log(counter.next()); // {value: 3, done: false} |
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* printer() { | |
console.log("We are starting!"); | |
console.log(yield); | |
console.log(yield); | |
console.log(yield); | |
console.log("We are done!"); | |
} | |
const counter = printer(); | |
counter.next(1); // We are starting! | |
counter.next(2); // 2 |
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 makeCounter() { | |
var count = 1; | |
return function () { | |
return count++; | |
} | |
} | |
var counter = makeCounter(); | |
console.log(counter()); // 1 | |
console.log(counter()); // 2 | |
console.log(counter()); // 3 |
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 printer(start) { | |
let state = 0; | |
let done = false; | |
function go(input) { | |
let result; | |
switch (state) { | |
case 0: | |
console.log("We are starting!"); | |
state = 1; | |
break; |
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* adder(initialValue) { | |
let sum = initialValue; | |
while (true) { | |
sum += yield sum; | |
} | |
} |
OlderNewer