Skip to content

Instantly share code, notes, and snippets.

@vkareh
Created November 14, 2014 19:33
Show Gist options
  • Save vkareh/c07b0548793eebe5289e to your computer and use it in GitHub Desktop.
Save vkareh/c07b0548793eebe5289e to your computer and use it in GitHub Desktop.
Wrapper around JSON.parse and JSON.stringify to make them run asynchronously. Also gets rid of having to use a try/catch on JSON.parse()
module.exports = {};
module.exports.parse = function parse() {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var _this = this;
process.nextTick(function() {
try {
var obj = JSON.parse.apply(_this, args);
return callback.apply(_this, [null, obj]);
} catch (err) {
return callback.apply(_this, [err]);
}
});
};
module.exports.stringify = function stringify() {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var _this = this;
process.nextTick(function() {
var text = JSON.stringify.apply(_this, args);
callback.apply(_this, [null, text]);
});
};
@kyriesent
Copy link

nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment