Skip to content

Instantly share code, notes, and snippets.

@mic-css
Last active May 6, 2020 10:26
Show Gist options
  • Save mic-css/1e32241a1cd9e6088b8a8a72f474c169 to your computer and use it in GitHub Desktop.
Save mic-css/1e32241a1cd9e6088b8a8a72f474c169 to your computer and use it in GitHub Desktop.
Custom wrapper function for destructuring asynchronous requests into loading, error and result states
export const asyncWrap = (promise: Promise<any>) =>
promise
.then(result => [false, null, result])
.catch(err => [false, err, null])
// Usage
const [isLoading = true, err, result] = asyncWrap(asynchronousTask())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment