Skip to content

Instantly share code, notes, and snippets.

View mightyguava's full-sized avatar

Yunchi Luo mightyguava

View GitHub Profile
@mightyguava
mightyguava / callback-hell.js
Created March 12, 2017 18:39
Demystifying Async Programming in Javascript - Callback Hell
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!');
});
});
});
})
@mightyguava
mightyguava / fetchJson-callbacks.js
Created March 12, 2017 18:42
Demystifying Async Programming in Javascript - fetchJson callbacks
// 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);
@mightyguava
mightyguava / fetchJson-promise.js
Created March 12, 2017 18:43
Demystifying Async Programming in Javascript - fetchJson promise
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);
});
@mightyguava
mightyguava / fetchJson-promise-fixed.js
Created March 12, 2017 18:43
Demystifying Async Programming in Javascript - fetchJson promise fixed
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))]
@mightyguava
mightyguava / fetchJson-async-generator.js
Created March 12, 2017 18:44
Demystifying Async Programming in Javascript - fetchJson async generator
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);
});
@mightyguava
mightyguava / generator-counts.js
Created March 12, 2017 18:46
Demystifying Async Programming in Javascript - Generator counts()
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}
@mightyguava
mightyguava / generator-printer.js
Created March 12, 2017 18:47
Demystifying Async Programming in Javascript - Generator printer()
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
@mightyguava
mightyguava / generator-internals-makeCounter-closure.js
Last active March 12, 2017 18:48
Demystifying Async Programming in Javascript - Generator internals makeCounter() closure
function makeCounter() {
var count = 1;
return function () {
return count++;
}
}
var counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3
@mightyguava
mightyguava / generator-internals-printer-state-machine.js
Created March 12, 2017 18:49
Demystifying Async Programming in Javascript - Generator internals printer state machine
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;
@mightyguava
mightyguava / generator-internals-adder.js
Created March 12, 2017 18:50
Demystifying Async Programming in Javascript - Generator internals adder
function* adder(initialValue) {
let sum = initialValue;
while (true) {
sum += yield sum;
}
}