Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 27, 2020 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/fc170d1fe63ae92677397e08dc888461 to your computer and use it in GitHub Desktop.
Save codecademydev/fc170d1fe63ae92677397e08dc888461 to your computer and use it in GitHub Desktop.
Codecademy export
const {checkAvailability} = require('./library.js');
const onFulfill = (itemsArray) => {
console.log(`Items checked: ${itemsArray}`);
console.log(`Every item was available from the distributor. Placing order now.`);
};
const onReject = (rejectionReason) => {
console.log(rejectionReason);
};
// Write your code below:
const checkSunglasses = checkAvailability('sunglasses', 'Favorite Supply Co.');
const checkPants = checkAvailability('pants', 'Favorite Supply Co.');
const checkBags = checkAvailability('bags', 'Favorite Supply Co.');
let myPromise = Promise.all([checkSunglasses, checkPants, checkBags]);
myPromise
.then((arrayOfValues) => {
onFulfill(arrayOfValues);
})
.catch((rejectReason) => {
onReject(rejectReason);
});
const checkAvailability = (itemName, distributorName) => {
console.log(`Checking availability of ${itemName} at ${distributorName}...`);
return new Promise((resolve, reject) => {
setTimeout(() => {
if (restockSuccess()) {
console.log(`${itemName} are in stock at ${distributorName}`)
resolve(itemName);
} else {
reject(`Error: ${itemName} is unavailable from ${distributorName} at this time.`);
}
}, 1000);
});
};
module.exports = { checkAvailability };
// This is a function that returns true 80% of the time
// We're using it to simulate a request to the distributor being successful this often
function restockSuccess() {
return (Math.random() > .2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment