Skip to content

Instantly share code, notes, and snippets.

@icodesido
Created January 16, 2019 17:17
Show Gist options
  • Save icodesido/626d26d9f33fc0e7c4cec4cbd92ea518 to your computer and use it in GitHub Desktop.
Save icodesido/626d26d9f33fc0e7c4cec4cbd92ea518 to your computer and use it in GitHub Desktop.
catch errors on multiple awaits
async function connect() {
const
connection = await asyncDBconnect('http://localhost:1234'),
session = await asyncGetSession(connection),
user = await asyncGetUser(session),
log = await asyncLogAccess(user);
return true;
}
// higher-order function to catch errors
function catchErrors(fn) {
return function (...args) {
return fn(...args).catch(err => {
console.log('ERROR', err);
});
}
}
(async () => {
await catchErrors(connect)();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment