Last active
August 29, 2015 13:59
-
-
Save emartinez-usgs/10785128 to your computer and use it in GitHub Desktop.
Asynchronous Javascript Development
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 getContent = function () { | |
return 'Hello World!'; | |
}; | |
console.log(getContent()); // 'Hello World!' |
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 getContent = function (callback) { | |
callback('Hello World!'); | |
}; | |
getContent(console.log); // 'Hello World!' |
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 getContent = function (callback) { | |
Xhr.ajax({ | |
url: 'hello.txt', | |
success: callback, | |
error: function () { | |
// Whoa error! | |
} | |
}); | |
}; | |
getContent(console.log); // 'Hello World!' |
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 getContent = function () { | |
return new Promise(function (fulfill, reject) { | |
Xhr.ajax({ | |
url: 'hello.txt', | |
success: fulfill, | |
error: reject | |
}); | |
}); | |
}; | |
getContent().then(console.log); |
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 getContent = function () { | |
return new Promise(function (fulfill, reject) { | |
Xhr.ajax({ | |
url: 'noexist.txt', | |
success: fulfill, | |
error: function (error, xhr) { | |
reject(error + ': ' + xhr.statusText); | |
} | |
}); | |
}); | |
}; | |
getContent() | |
.then(console.log) | |
.catch(console.log) | |
.then(function () { | |
console.log('Complete!'); | |
}); |
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 files = ['file1.txt', 'file2.txt', 'file3.txt']; | |
var getContent = function (url) { | |
return new Promise(function (fulfill, reject) { | |
Xhr.ajax({ | |
url: url, | |
success: fulfill, | |
error: reject | |
}); | |
}); | |
}; | |
Promise.all(files.map(getContent)) | |
.then(function (values) { | |
values.forEach(console.log); | |
}) | |
.catch(console.log) | |
.then(function () { | |
console.log('Complete!'); | |
}); |
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
<!-- Placeholder Gist for Name Only --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment