Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created July 25, 2017 02:53
Show Gist options
  • Save prof3ssorSt3v3/4d7246133bb0e2638eaf69fc7a33be75 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/4d7246133bb0e2638eaf69fc7a33be75 to your computer and use it in GitHub Desktop.
//Promises - Just the Basic Facts
//wrappers for anything async
// ajax calls, reading files, timeouts, geolocation, talk to a database, or anything that would use a callback function
//use them to get rid of callback hell
//fetch() returns a Promise.
//var result = multiplyTwoNumbers(5, 10);
//console.log(result); //50
//
//var photo = downloadPhoto('http://localhost/cat.jpg');
// photo would be 'undefined'
const rand = () => Math.floor(Math.random() * 10) + 1;
let p1 = new Promise((resolve, reject) => {
let x = 5;
//resolve(x); //calling this will call then()
//reject(x);
let num = rand();
setTimeout(resolve, 1500, num)
})
//fetch().then().then().catch()
p1.then((ex) => {
console.log(ex);
return ex * 2;
}).then( (x) => {
console.log(x);
}).catch( (exx) => {
console.log('caught', exx);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment