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 pletion of the long-running task, however, JSON.stringify() doesn't provide the option for a callback.
How should I write this?
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 pletion of the long-running task, however, JSON.stringify() doesn't provide the option for a callback.
How should I write this?
Share Improve this question edited Jan 4, 2014 at 3:45 max asked Jan 4, 2014 at 2:53 maxmax 6178 silver badges20 bronze badges 4- Why would you want it to be async? Are user setting that big? – Nathaniel Johnson Commented Jan 4, 2014 at 3:00
-
If you want it to be async, use web workers or
setTimeout
. – Qantas 94 Heavy Commented Jan 4, 2014 at 3:10 - Nope, settings aren't big right now. But if it were async and I relied on the fact that it executed quickly while small, then should it ever take much longer to execute it would be a tricky bug to track down. – max Commented Jan 4, 2014 at 3:39
- Async APIs usually either have a callback function passed as a parameter, or return a Promise. In either case, they wouldn't have a value ever directly available via the function return. So, it wouldn't work only some times. – WiredPrairie Commented Jan 4, 2014 at 13:21
1 Answer
Reset to default 6JSON.stringify is not asynchronous so the $http.post line won't execute until stringification is done.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365461a4624556.html
评论列表(0条)