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 p = new Promise(function(resolve, reject){ | |
foo.bar(); // `foo` is not defined, so error! | |
resolve(374); // never gets here :( | |
}); | |
p.then( | |
function fulfilled(){ | |
// never gets here :( | |
}, | |
function rejected(err){ |
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 p = new Promise( function(resolve,reject){ | |
resolve(374); | |
}); | |
p.then(function fulfilled(message){ | |
foo.bar(); | |
console.log(message); // never reached | |
}, | |
function rejected(err){ | |
// never reached |
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 p = Promise.resolve(374); | |
p.then(function fulfilled(msg){ | |
// numbers don't have string functions, | |
// so will throw an error | |
console.log(msg.toLowerCase()); | |
}) | |
.done(null, function() { | |
// If an exception is caused here, it will be thrown globally | |
}); |
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
// Just a standard JavaScript function | |
function getNumber1() { | |
return Promise.resolve('374'); | |
} | |
// This function does the same as getNumber1 | |
async function getNumber2() { | |
return 374; | |
} |
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 f1() { | |
return Promise.reject('Some error'); | |
} | |
async function f2() { | |
throw 'Some error'; | |
} |
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
async function loadData() { | |
// `rp` is a request-promise function. | |
var promise1 = rp('https://api.example.com/endpoint1'); | |
var promise2 = rp('https://api.example.com/endpoint2'); | |
// Currently, both requests are fired, concurrently and | |
// now we'll have to wait for them to finish | |
var response1 = await promise1; | |
var response2 = await promise2; | |
return response1 + ' ' + response2; |
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 loadData = async function() { | |
// `rp` is a request-promise function. | |
var promise1 = rp('https://api.example.com/endpoint1'); | |
var promise2 = rp('https://api.example.com/endpoint2'); | |
// Currently, both requests are fired, concurrently and | |
// now we'll have to wait for them to finish | |
var response1 = await promise1; | |
var response2 = await promise2; | |
return response1 + ' ' + response2; |
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
// `rp` is a request-promise function. | |
rp(‘https://api.example.com/endpoint1').then(function(data) { | |
// … | |
}); |
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
// `rp` is a request-promise function. | |
var response = await rp(‘https://api.example.com/endpoint1'); |
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 loadData() { | |
try { // Catches synchronous errors. | |
getJSON().then(function(response) { | |
var parsed = JSON.parse(response); | |
console.log(parsed); | |
}).catch(function(e) { // Catches asynchronous errors | |
console.log(e); | |
}); | |
} catch(e) { | |
console.log(e); |