Last active
December 18, 2020 23:57
-
-
Save destroytoday/b4c035b2ceeabe67b74f67fc91128c22 to your computer and use it in GitHub Desktop.
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
/* | |
Typescript erroring: | |
--- | |
Type 'Promise<(T | undefined)[] | [any, undefined]>' is not assignable to type 'Promise<[void | Error, void | T]>'. | |
Type '(T | undefined)[] | [any, undefined]' is not assignable to type '[void | Error, void | T]'. | |
Type '(T | undefined)[]' is not assignable to type '[void | Error, void | T]'. | |
Target requires 2 element(s) but source may have fewer.ts(2322) | |
*/ | |
export default function <T> (promise: Promise<T>): Promise<[Error | void, T | void]> { | |
return promise | |
.then((data) => ([undefined, data])) | |
.catch((error) => ([error, undefined])) | |
} | |
/* | |
Usage: | |
const [err, res] = await handle(Axios.get('/api/foo')) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed! Thanks, @dlo! (https://twitter.com/dwlz/status/1340082549369036800)