javascript - Asynchronous JSON.stringify - Stack Overflow

If I have: fetched_user.settings = null;fetched_user.settings = JSON.stringify(settings);$http.post(&

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
Add a ment  | 

1 Answer 1

Reset to default 6

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365461a4624556.html

相关推荐

  • javascript - Asynchronous JSON.stringify - Stack Overflow

    If I have: fetched_user.settings = null;fetched_user.settings = JSON.stringify(settings);$http.post(&

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信