Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
async-json
Advanced tools
Readme
async-json is a library that provides an asynchronous version of the standard JSON.stringify.
This can be used in both the browser and in node.js. The only dependency is that JSON
must exist either by the
implementation providing it (all modern browsers and implementations) or by providing it yourself
json2.js.
Assuming you are using node.js:
The easiest way to install is through npm.
$ npm install escort
Alternatively, you can pull from github and place where necessary.
There is only one, very simple function: stringify.
It takes a javascript object as its first argument and a callback as its second.
The callback has two parameters and follows the node.js style.
null
if there is no error.var asyncJSON = require('async-json');
asyncJSON.stringify({ some: "data" }, function (err, jsonValue) {
if (err) {
throw err;
}
jsonValue === '{"some":"data"}';
});
In the previous case, the callback is invoked immediately, as no asynchronous functions were found.
In the first argument, if a function is found, rather than skipping it as JSON
does, it is invoked.
Two types of functions can be provided:
Synchronous functions are useful for handling a value on-the-fly that you do not wish to precompute.
var asyncJSON = require('async-json');
asyncJSON.stringify({ some: function() { return "hard to compute data"; } }, function (err, jsonValue) {
if (err) {
throw err;
}
jsonValue === '{"some":"hard to compute data"}';
});
Like the previous example, this is invoked immediately, and the function will be invoked along the pipeline. Since JavaScript lacks the concept of background threads, this may cause the program to freeze while the value is being computed.
Note: it is perfectly acceptable to return an object that contains another function, it will be calculated through the same process.
Asynchronous functions are useful for handling I/O driven data or if you have a gradual calculation mechanism.
var asyncJSON = require('async-json');
asyncJSON.stringify({ data: function(callback) {
SomeDatabase.find({}, function (err, value) {
if (err) {
callback(err);
} else {
callback(null, {
key: value.someNumber
});
}
});
} }, function (err, jsonValue) {
if (err) {
throw err;
}
jsonValue === '{"data":{"key":12345}}';
});
This fetches data from some database or cache or other external source, and once it has a resultant value or error, will call the async callback function.
var asyncJSON = require('async-json');
asyncJSON.stringify({ data: function(callback) {
setTimeout(function () {
callback(null, "Hello there!");
}, 1000);
} }, function (err, jsonValue) {
if (err) {
throw err;
}
jsonValue === '"Hello there!"';
});
This will invoke the async callback one second after calling stringify
with the obvious result.
first:
$ git submodule update --init
then:
$ make test
If you find any issues with async-json or have any suggestions or feedback, please feel free to visit the github issues page.
MIT licensed. See LICENSE for more details.
FAQs
An asynchronous version of JSON.stringify
The npm package async-json receives a total of 58 weekly downloads. As such, async-json popularity was classified as not popular.
We found that async-json demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.