Created
November 14, 2014 19:33
-
-
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()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice.