Skip to content

Instantly share code, notes, and snippets.

@nbhaohao
Created April 4, 2020 14:40
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 nbhaohao/1e36523792338fc5b5cdafb1617a7cd0 to your computer and use it in GitHub Desktop.
Save nbhaohao/1e36523792338fc5b5cdafb1617a7cd0 to your computer and use it in GitHub Desktop.
async/await 优雅的错误处理方法
function awaitWrap<T, U = any>(promise: Promise<T>): Promise<[U | null, T | null]> {
return promise
.then<[null, T]>((data: T) => [null, data])
.catch<[U, null]>(err => [err, null])
}
@nbhaohao
Copy link
Author

nbhaohao commented Apr 4, 2020

转载自:
作者:Vincent.W
链接:https://juejin.im/post/5c49eb28f265da613a545a4b
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment