Skip to content

Instantly share code, notes, and snippets.

@dlo
Forked from destroytoday/handle.ts
Created December 18, 2020 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlo/012fb10ce27e8426c633bb70f106009e to your computer and use it in GitHub Desktop.
Save dlo/012fb10ce27e8426c633bb70f106009e to your computer and use it in GitHub Desktop.
/*
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