JSON.tryParse with Promise

9 בSeptember 2015

no comments

JSON.parse is a method you must have used one or twice. And you must remember that it raises exception, as almost every parsing string in case the string comes in a wrong format.

So, how about having a function that returns promise, so you can safely parse JSON’s  and receive its result on the promise’s “then” method.

Here’s some small code example:

 JSON.tryParse('{"a" : 1}').then(function(result) {
//do something with the result
}, function(error)
{
   //do something with the error
});

And here’s the link to its gist:

https://gist.github.com/ranwahle/2d38730af21909dd70a2

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*