This file contains hidden or 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
listen('click', function (e){ | |
setTimeout(function(){ | |
ajax('https://api.example.com/endpoint', function (text){ | |
if (text == "hello") { | |
doSomething(); | |
} | |
else if (text == "world") { | |
doSomethingElse(); | |
} | |
}); |
This file contains hidden or 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
listen('click', function (e) { | |
// .. | |
}); |
This file contains hidden or 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
setTimeout(function(){ | |
// .. | |
}, 500); |
This file contains hidden or 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
ajax('https://api.example.com/endpoint', function (text){ | |
// .. | |
}); |
This file contains hidden or 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
if (text == "hello") { | |
doSomething(); | |
} | |
else if (text == "world") { | |
doSomethingElse(); | |
} |
This file contains hidden or 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 x = 1; | |
var y = 2; | |
console.log(x + y); |
This file contains hidden or 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 sum(getX, getY, callback) { | |
var x, y; | |
getX(function(result) { | |
x = result; | |
if (y !== undefined) { | |
callback(x + y); | |
} | |
}); | |
getY(function(result) { | |
y = result; |
This file contains hidden or 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 sum(xPromise, yPromise) { | |
// `Promise.all([ .. ])` takes an array of promises, | |
// and returns a new promise that waits on them | |
// all to finish | |
return Promise.all([xPromise, yPromise]) | |
// when that promise is resolved, let's take the | |
// received `X` and `Y` values and add them together. | |
.then(function(values){ | |
// `values` is an array of the messages from the |
This file contains hidden or 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
sum(fetchX(), fetchY()) | |
.then( | |
// fullfillment handler | |
function(sum) { | |
console.log( sum ); | |
}, | |
// rejection handler | |
function(err) { | |
console.error( err ); // bummer! | |
} |
This file contains hidden or 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 delay(time) { | |
return new Promise(function(resolve, reject){ | |
setTimeout(resolve, time); | |
}); | |
} | |
delay(1000) | |
.then(function(){ | |
console.log("after 1000ms"); | |
return delay(2000); |