Created
December 19, 2019 14:44
-
-
Save skulptur/7406e281919d78a12c3fba8b3e1b1cd1 to your computer and use it in GitHub Desktop.
parse json safely using promises
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 parseJSON = <T = {}>(json: string) => { | |
return new Promise<T>((resolve, reject) => { | |
try { | |
resolve(JSON.parse(json)); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment