Last active
May 6, 2020 10:26
-
-
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
This file contains 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
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