Created
February 23, 2018 07:59
-
-
Save tomcask/e95933023ab4777d548cece7400be7b0 to your computer and use it in GitHub Desktop.
try to avoid use try catch statement and manage the error in await call
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
const to = promise =>{ | |
return promise.then(data =>{ | |
return [null, data] | |
}) | |
.catch(err => [err]) | |
} | |
fetchData(){ | |
let err, data | |
[err, data] = await to(Api.getEntity()) | |
if (err) { throw new Error(err)} | |
return new Entity(data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment