-
-
Save codecademydev/fc170d1fe63ae92677397e08dc888461 to your computer and use it in GitHub Desktop.
Codecademy export
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
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); | |
}); | |
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
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