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) { | |
let state = 0; | |
let done = false; | |
function go() { | |
let result; | |
switch (state) { | |
case 0: | |
result = start + 1; | |
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 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; | |
} | |
} |
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 state = 'initial'; | |
let done = false; | |
let sum = initialValue; | |
function go(input) { | |
let result; | |
switch (state) { | |
case 'initial': | |
result = initialValue; | |
state = 'loop'; |
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; | |
let lastSum = initialValue; | |
let temp; | |
while (true) { | |
try { | |
temp = sum; | |
sum += yield sum; | |
lastSum = temp; | |
} catch (e) { |
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 state = 'initial'; | |
let done = false; | |
let sum = initialValue; | |
let lastSum; | |
let temp; | |
function go(input, err) { | |
let result; |
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
async function () { | |
var user = await fetchJson('/api/user/self'); | |
var interests = await fetchJson('/api/user/interests?userId=' + self.id); | |
var recommendations = await 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 deferred(val) { | |
return new Promise((resolve, reject) => resolve(val)); | |
} | |
co(function* asyncAdds() { | |
console.log(yield deferred(1)); // 1 | |
console.log(yield deferred(2)); // 2 | |
console.log(yield deferred(3)); // 3 | |
return 4; | |
}).then(function (result) { |
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 co(generator) { | |
return new Promise((resolve, reject) => { | |
const g = generator(); | |
function next(nextVal) { | |
const ret = g.next(nextVal); | |
if (ret.done) { | |
return resolve(ret.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
function deferred(val) { | |
return new Promise((resolve, reject) => resolve(val)); | |
} | |
function deferReject(e) { | |
return new Promise((resolve, reject) => reject(e)); | |
} | |
co(function* asyncAdds() { | |
console.log(yield deferred('We are starting!')); |