JSON.tryParse with Promise

9 בSeptember 2015

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
no comments