Skip to content

Instantly share code, notes, and snippets.

@PetKatt
Created October 9, 2018 15:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PetKatt/38185a5892d608b4edeb4291f138223d to your computer and use it in GitHub Desktop.
Save PetKatt/38185a5892d608b4edeb4291f138223d to your computer and use it in GitHub Desktop.
Promises in javascript - simulation of data fetching request
// 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