max
max

Reputation: 617

Asynchronous JSON.stringify

If I have:

fetched_user.settings = null;
fetched_user.settings = JSON.stringify(settings);
$http.post('/api_endpoint', {
  val: fetched_user.settings
});

And JSON.stringify takes forever to execute, then my understanding is val: fetched_user.settings will likely be null since this is executed asynchronously (async newbie checkpoint: is this correct?).

Normally in these situations I would supply a callback function to be executed on completion of the long-running task, however, JSON.stringify() doesn't provide the option for a callback.

How should I write this?

Upvotes: 1

Views: 4144

Answers (1)

Robert Levy
Robert Levy

Reputation: 29083

JSON.stringify is not asynchronous so the $http.post line won't execute until stringification is done.

Upvotes: 5

Related Questions