How to promisify correctly JSON.parse method with bluebird
Solution 1
Promise.promisify
is thought for asynchronous functions that take a callback function. JSON.parse
is no such function, so you cannot use promisify
here.
If you want to create a promise-returning function from a function that might throw
synchronously, Promise.method
is the way to go:
var parseAsync = Promise.method(JSON.parse);
…
parseAsync(data).then(…);
Alternatively, you will just want to use Promise.resolve
to start your chain:
Promise.resolve(data).then(JSON.parse).then(…);
Solution 2
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)
Solution 3
First of all, JSON.parse
is not an asynchronous function. So, don't try to promisify it.
Because I want to create a chain of promises where JSON.parse stand at the top
Then, simply create a Promise resolved with the parsed JSON object, like this
Promise.resolve(JSON.parse(data))
.then(...)
Now, to your actual question, you are getting the error,
Unhandled rejection Error: object
because, if your chain of promises is rejected, you are not handling it. So, don't forget to attach a catch handler, like this
Promise.resolve(JSON.parse(data))
.then(...)
.catch(...)
READ THIS There is a problem with the approach I have shown here, as pointed out by Bergi, in the comments. If the JSON.parse
call fails, then the error will be thrown synchronously and you may have to write try...catch
around the Promise
code. Instead, one would write it as Bergi suggested in his answer, to create a Promise object with just the data, and then do JSON.parse
on that Promise chain.
Mazzy
Updated on July 09, 2022Comments
-
Mazzy almost 2 years
I'm trying to promisify
JSON.parse
method but unfortunately without any luck. This is my attempt:Promise.promisify(JSON.parse, JSON)(data).then((result: any) => {...
but I get the following error
Unhandled rejection Error: object
-
Bergi over 8 years-1 for not catching exceptions from the
JSON.parse()
call. -
thefourtheye over 8 years@Bergi To think of it, my answer it totally useless. Thanks for pointing out.
-
Kurt M over 6 yearsJSON.parse does throw exceptions for syntacticly incorrect input: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Please edit your answer to remove incorrect information
-
mtkopone over 6 yearsYes, of course it does. That's why the try/catch is there. And that's why it is handled by rejecting the promise, so that the promise-wrapped version of parsing JSON does not throw synchronous exceptions, but instead rejects the promise, as it should in promise-land.
-
Kurt M over 6 yearsI'm sorry. I miss read your first sentence. Reading it again and I see your answer is fine. Apologies.
-
Ignacio Segura over 6 yearsPromise.resolve(...) worked like a charm for me. Thanks!