Created
October 9, 2018 15:15
-
-
Save PetKatt/38185a5892d608b4edeb4291f138223d to your computer and use it in GitHub Desktop.
Promises in javascript - simulation of data fetching request
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
// SIMULATION OF A DATA FETCHING REQUEST WITH PROMISE | |
// Fulfilled Promise (data fetched successfully) | |
var promise1 = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve("Promise resolved"); | |
}, 300); | |
setTimeout(function() { | |
reject("Promise Rejected"); | |
}, 500); | |
}); | |
promise1.then(function(res) { | |
console.log("Promise1: " + res); | |
}); | |
// Rejected Promise (data fetched failed with error msg) | |
var promise2 = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve("Promise resolved"); | |
}, 500); | |
setTimeout(function() { | |
reject("Promise Rejected"); | |
}, 300); | |
}); | |
promise2.then(function(res) { | |
console.log("Promise2: " + res); | |
}, function(err) { | |
console.log("Promise2: " + err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment