I\'m trying to promisify JSON.parse
method but unfortunately without any luck. This is my attempt:
Promise.promisify(JSON.parse, JSON)(data).then((r
Late to the party, but I can totally understand why you might want a promisified JSON parse method which never throws exceptions. If for nothing else, then to remove boilerplate try/catch-handling from your code. Also, I see no reason why synchronous behavior shouldn't be wrapped in promises. So here:
function promisedParseJSON(json) {
return new Promise((resolve, reject) => {
try {
resolve(JSON.parse(json))
} catch (e) {
reject(e)
}
})
}
Usage, e.g:
fetch('/my-json-doc-as-string')
.then(promisedParseJSON)
.then(carryOn)
.catch(dealWithIt)