Skip to content

Instantly share code, notes, and snippets.

@skulptur
Created December 19, 2019 14:44
Show Gist options
  • Save skulptur/7406e281919d78a12c3fba8b3e1b1cd1 to your computer and use it in GitHub Desktop.
Save skulptur/7406e281919d78a12c3fba8b3e1b1cd1 to your computer and use it in GitHub Desktop.
parse json safely using promises
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