Last active
November 26, 2017 21:11
-
-
Save sadra/60429ffd4d5f1d8e7b0533c66d7f510d to your computer and use it in GitHub Desktop.
Mom Promise to but new phone.
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
/* ES5 */ | |
var isMomHappy = true; | |
// Promise | |
var willIGetNewPhone = new Promise( | |
function (resolve, reject) { | |
if (isMomHappy) { | |
var phone = { | |
brand: 'iPhone X', | |
color: 'gray' | |
}; | |
resolve(phone); // fulfilled | |
} else { | |
var reason = new Error('Mom is angry.'); | |
reject(reason); // reject | |
} | |
} | |
); | |
// 2nd promise | |
var showNewPhone = function (phone) { | |
return new Promise( | |
function (resolve, reject) { | |
var message = 'Hey friend, I have a new ' + | |
phone.color + ' ' + phone.brand + '.'; | |
resolve(message); | |
} | |
); | |
}; | |
// call our promise | |
var askMom = function () { | |
console.log('before asking Mom'); // log before | |
willIGetNewPhone | |
.then(showNewPhone) | |
.then(function (fulfilled) { | |
// yay, you got a new phone | |
console.log(fulfilled); | |
// output: { brand: 'iPhone X', color: 'gray' } | |
}) | |
.catch(function (error) { | |
// ops, mom don't buy it | |
console.log(error.message); | |
// output: 'Mom is angry.' | |
}); | |
console.log('after asking mom'); // log after | |
}; | |
askMom(); |
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
/* ES6 */ | |
let isMomHappy = true; | |
//Promise | |
const willGetNewPhone = new Promise( | |
function(resolve, reject) { | |
if(isMomHappy) { | |
let phone = { | |
brand: 'iPhone X', | |
color: 'gray' | |
}; | |
resolve(phone); //fulfilled | |
} else { | |
let reason = new Error('Mom is angry.') | |
reject(reason); //reject | |
} | |
} | |
); | |
const showNewPhone = function (phone) { | |
let message = 'Hey friend, I have a new ' + phone.color + ' ' + phone.brand +'.'; | |
return Promise.resolve(message) | |
} | |
// call our promise | |
const askMom = function () { | |
console.log('before asking Mom'); // log before | |
willGetNewPhone | |
.then(showNewPhone) | |
.then( function (fulfilled) { | |
//yay, you got a new phone | |
console.log(fulfilled); | |
//output: 'Hey friend, I have a new gray iPhone X.' | |
}).catch( function (error) { | |
//oops, mom don't buy it | |
console.log(error.message); | |
//output: 'Mom is angry.' | |
}); | |
console.log('after asking mom'); // log after | |
} | |
askMom(); |
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
/* ES7 */ | |
const isMomHappy = true; | |
// Promise | |
const willIGetNewPhone = new Promise( | |
(resolve, reject) => { | |
if (isMomHappy) { | |
const phone = { | |
brand: 'iPhone X', | |
color: 'gray' | |
}; | |
resolve(phone); | |
} else { | |
const reason = new Error('Mom is angry.'); | |
reject(reason); | |
} | |
} | |
); | |
// 2nd promise | |
async function showNewPhone(phone) { | |
return new Promise( | |
(resolve, reject) => { | |
var message = 'Hey friend, I have a new ' + | |
phone.color + ' ' + phone.brand + '.'; | |
resolve(message); | |
} | |
); | |
}; | |
// call our promise | |
async function askMom() { | |
try { | |
console.log('before asking Mom'); | |
let phone = await willIGetNewPhone; | |
let message = await showNewPhone(phone); | |
console.log(message); | |
console.log('after asking mom'); | |
} | |
catch (error) { | |
console.log(error.message); | |
} | |
} | |
(async () => { | |
await askMom(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment