Last active
October 7, 2019 19:49
-
-
Save mistercoffee66/98bf07996c1a23f62c9529e5dac5cfa1 to your computer and use it in GitHub Desktop.
async/await error scenarios and variations
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
/* eslint no-console: 0 */ | |
/* eslint no-unused-vars: 0 */ | |
const https = require('https'); | |
const goodUrl = 'https://jsonplaceholder.typicode.com/users/1'; | |
const badUrl = 'https://jsonplaceholder.typicode.com/foo'; | |
const badDomain = 'https://no.such.url'; | |
const externalCall = (url, t = 0) => new Promise((resolve, reject) => { | |
setTimeout(() => { | |
https.get(url, (res) => { | |
let str = ''; | |
res.on('data', (d) => { | |
str += d; | |
}); | |
res.on('error', (e) => { | |
reject(Error(`error from promise reject:\n${e}`)); | |
}); | |
res.on('end', () => { | |
if (res.statusCode !== 200) { | |
reject(Error(`error from promise reject:\n${res.statusCode}`)); | |
} | |
resolve(JSON.parse(str)); | |
}); | |
}).on('error', (e) => { | |
reject(Error(`error from promise reject:\n${e}`)); | |
}); | |
}, t); | |
}); | |
const runSeries = async() => { | |
// series | |
const data1 = await externalCall(goodUrl, 500); | |
const data2 = await externalCall(goodUrl, 1000); | |
return [data1, data2]; | |
// | |
// series error is handled | |
// const data1 = await externalCall(goodUrl); | |
// const data2 = await externalCall(badDomain, 1000); | |
// return [data1, data2]; | |
// | |
// series error is handled | |
// const data1 = await externalCall(badDomain); | |
// const data2 = await externalCall(badDomain); | |
// return [data1, data2]; | |
}; | |
const runParallel = async() => { | |
// parallel | |
// const data1 = externalCall(goodUrl, 500); | |
// const data2 = externalCall(goodUrl, 1000); | |
// return [await data1, await data2]; | |
// | |
// parallel error is handled | |
// const data1 = delayed(goodUrl); | |
// const data2 = delayed(badDomain); | |
// return [await data1, await data2]; | |
// | |
// parallel error is not handled | |
// const data1 = externalCall(badDomain); | |
// const data2 = externalCall(badDomain); | |
// return [await data1, await data2]; | |
// | |
// better parallel | |
const data1 = externalCall(goodUrl, 500); | |
const data2 = externalCall(goodUrl, 1000); | |
return Promise.all([data1, data2]); | |
// | |
// better parallel error is handled | |
// const data1 = externalCall(badDomain); | |
// const data2 = externalCall(badDomain); | |
// return Promise.all([data1, data2]); | |
}; | |
const api = async() => { | |
// these two are equivalent | |
// try { | |
// return await runParallel(); | |
// } catch (e) { | |
// return `error from try/catch: ${e}`; | |
// } | |
// return runSeries().catch(e => (`error from .catch():\n${e}`)); | |
return runParallel().catch(e => (`error from .catch():\n${e}`)); | |
}; | |
// let's pretend this is an api route called from somewhere | |
const route = () => { | |
const now = new Date(); | |
api().then((data) => { | |
console.log(data); | |
console.log(`${new Date() - now}ms`); | |
}); | |
}; | |
route(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment