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 eventLoop() { | |
// perform loop ticks, "forever" | |
while (true) { | |
var tastQueue = selectTaskQueue(); | |
var nextTask = taskQueue.getNextTask(); | |
nextTask(); | |
} | |
} |
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 getThing(arg) { | |
return getStuff(arg) | |
.then(getMoreStuff) | |
.then(getThatFinalThing); | |
} |
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 getThing(arg) { | |
return new Promise(function(resolve, reject) { | |
getStuff(arg) | |
.then(function(stuff) { | |
getMoreStuff(stuff) | |
.then(function(moreStuff) { | |
getThatFinalThing(moreStuff) | |
.then(resolve, reject) | |
}, reject) | |
}, reject) |
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
promise | |
.then( | |
function(){ | |
throw Error('example') | |
}, | |
function () { | |
console.log('This will not be called'); | |
} | |
) | |
.catch(function (err) { |
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 promise1 = httpGetPromise(url1); | |
var promise2 = httpGetPromise(url2); | |
var promise3 = httpGetPromise(url3); | |
promise1 | |
.then(function (result1) { | |
processValue(result1); | |
}) | |
.then(function () { | |
return promise2; |
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* gen(s) { | |
console.log(s); | |
console.log('yield 1 - ' + (yield 'A')); | |
console.log('yield 2 - ' + (yield 'B')); | |
console.log('end'); | |
return 'C'; | |
} | |
var iter = gen(0); | |
console.log(iter.next('a')); |
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* gen() { | |
yield 'pause here'; | |
return 'I am done'; | |
} | |
// Calling the generator function returns an iterator. | |
var iter = gen(); | |
// Calling .next() on an iterator returns an object with a value and a done flag | |
iter.next(); // { value: any, done: boolean } |
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 getThing(arg, callback){ | |
getStuff(arg, function(err, stuff) { | |
if (err) { | |
callback(err); | |
} else { | |
getMoreStuff(stuff, function(err, moreStuff) { | |
if (err) { | |
callback(err); | |
} else { | |
getThatFinalThing(moreStuff, function(err, thing) { |
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 getJSON(url) { | |
return httpGetPromise(url) | |
.then(JSON.parse); | |
} |
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 promise = new Promise(function(resolve, reject) { | |
undefined.method(); // we cause an exception | |
}); | |
promise | |
.then( | |
function() { | |
// not called | |
}, | |
function(err) { |