< Back to /snippets

Async/Await error handling Higher Order Function.

Originally written by Wes Bos for his Async + Await talk.

const handleError = fn => (
  (...params) => fn(...params).catch(console.error)
);

// Wrap your async function in the error handler
const safeFunc = handleError(someAsyncFunc);

// Call the wrapped function, any errors are caught and logged
safeFunc();