Last active
April 25, 2018 06:17
-
-
Save max8hine/c6725f6ea0c638b32ab317ce487f268d to your computer and use it in GitHub Desktop.
the evolution of async function in JS
This file contains hidden or 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
// kick off both thing, one after another | |
/** | |
* Promise | |
*/ | |
const coffeePromise = makeCoffee() | |
const breakfastPromise = makeBreakfast() | |
// then once each are done, deal with them | |
coffeePromise.then(coffee => { | |
drinkCoffee() | |
}) | |
breakfastPromise.then(([egg, bacon]) => { | |
eat(eggs, bacon) | |
}) | |
// You can also wait until both are done | |
Promise | |
.all([coffeePromise, breakfastPromise]) | |
.then(([coffee, breakfast]) => { | |
sitDownWith(coffee, breakfast) | |
}) | |
// Example 2: | |
function sleep(amount) { | |
return new Promise((resolve, reject) => { | |
if (amount <= 300) return reject('That\'s too fast, cool it down!') | |
setTimeout(() => resolve(`slept for ${amount}`), amount) | |
}) | |
} | |
// To resolve value in Promise.then | |
sleep(500) | |
.then((result) => { | |
console.log(result) | |
return sleep(1000) | |
}) | |
.then((result) => { | |
console.log(result) | |
return sleep(750) | |
}) | |
.then((result) => { | |
console.log(result) | |
console.log('Done') | |
}) | |
// To resolve value in Async-Await syntax | |
async function goSleep() { | |
// it resolve promise sequentially, ☹️ | |
await sleep(500) | |
await sleep(1000) | |
await sleep(750) | |
await sleep('done') | |
} | |
// To resolve all the promise in the same time 😀 | |
Promise | |
.all([sleep(350), sleep(400), sleep(500) ]) | |
.then(([p1, p2, p3]) => { | |
console.log(p1, p2, p3) | |
}) | |
// Or in the Aync-Await Func 😂 | |
asnyc function allGoSleep() { | |
const response = Promise.all([sleep(350), sleep(400), sleep(500) ]) | |
console.log(response) | |
} | |
/* | |
* Error Handling | |
* ----------------- | |
*/ | |
// Try-Catch | |
async function areYouSleep() { | |
try { | |
const yes = await sleep(299) | |
} catch (err) { | |
throw Error('Have not done yet,', err) | |
} | |
} | |
// HOC, B/c async return a promise | |
// It born to be able to catch error | |
async function yolo() { | |
// do something that errors out | |
const wes = wait axios.get('https://no.com') | |
} | |
// make a function to handle that promise based error | |
function handleError(fn) { | |
return function (...params) { | |
return fn(...params).catch(function (err) { | |
// do something with the error | |
console.error(`Oops! ${err}`) | |
}) | |
} | |
} | |
// HOT SHOT IMPLICITY RETURN (WHAT) | |
const handleErr = fn => (...params) => fn(...params).catch(console.error) | |
/* | |
* In Action, Express App | |
* ---------------------- | |
*/ | |
const getOrders = async (req, res, next) => { | |
const orders = Orders.find({ email: req.user.email }) | |
if (!orders.length) throw Error('No Orders Found') | |
// ... | |
} | |
router.get('/orders', getOrders) | |
const displayErrors = async (error, req, res, next) => { | |
res.status(err.status || 500) | |
res.render('error', { error }) | |
} | |
// any time we call next('Something happened') displayErrors will kick in | |
app.use(displayErrors) | |
// but It doesn't handle unexprected errors, | |
// Such as syntax errors, database error, connection errors | |
const catchErrors = (fn) => { | |
return function (req, res, next) { | |
return fn(req, res, next).catch(next) | |
} | |
} | |
// or Hot Shot | |
const catchErrors = (fn) => (req, res, next) => fn(req, res, next).catch(next) | |
// This is how we implement it | |
router.get('/orders'j, catchErrors(getOrders)) | |
process.on('unhandledRejection', error => { | |
// could send out to your error handler | |
console.log('unhandledRejection', error) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment