Alexander  Danilov Alexander Danilov - 1 month ago 10
Javascript Question

Async stringify (serialize) to JSON in javascript

In my react-native app I need to stringify (serialize) big objects asynchronously and not to block UI thread.

Need something like this:

// logs "{foo:bar}"
JSON.stringifyAsync({ foo: "bar" }).then(x => console.log(x));


Please don't suggest to wrap JSON.stringify into Promise, it doesn't make it async.

Answer

I don't think this is currently possible. It's an issue for node.js (see here, here, and here) and I don't think the situation is any better for browsers or other JS engines.

There is an EcmaScript proposal (see here) to add functions JSON.parseAsync and JSON.stringifyAsync methods. I don't know the status of this proposal. From the proposal: "There is no way of transpilating the actual effect of this proposal." So it looks like the proposer(s) agree that there's currently no way to do this.