Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@kyriesent kyriesent commented Nov 14, 2014

nice.

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