简体   繁体   English

如何将同步ajax转换为异步?

[英]How do I convert synchronous ajax to asynchronous?

What I am trying to accomplish 我要完成的工作

I am trying to check the server time and compare it against preset closing time to prevent user from clicking on expired sale items (and display the item has expired) but I am hitting a snag on how to accomplish this when it's mixed in with synchronous call 我正在尝试检查服务器时间,并将其与预设的关闭时间进行比较,以防止用户单击过期的销售商品(并显示该商品已过期),但是当它与同步调用混合使用时,我遇到了一个麻烦

I know async:false is a very bad idea and it won't work in Firefox(works in Chrome and IE) but it does exactly what I wanted. 我知道async:false是一个非常糟糕的主意,它不能在Firefox中工作(在Chrome和IE中都可以工作),但它确实满足我的要求。 I am having a hard time figuring out how to convert my code to be asynchronous. 我很难弄清楚如何将我的代码转换为异步代码。 If I change part of my code asynchronous then all my other parts of code that depends on it falls apart because the timing is off. 如果我将代码的一部分更改为异步的,则依赖该代码的所有其他部分都会因为时机关闭而崩溃。

Previous Code - All I would call if sale_closed() 先前的代码 -如果sale_closed()

@get_server_time: (url) ->
$.ajax(
  url: url + '/servertime'
  type: 'GET'
  async: false
)

@sale_closed: () ->
   if not @is_valid_user
     return true
   if Config.override_bid_closed?
     return false
   if @get_server_time(url) > sale_end
     return true
   return false

What I tried to do but that's not working 我试图做的事,但是那没用

CoffeeScript CoffeeScript的

@get_server_time: (url) ->
$.ajax(
  url: url + '/servertime'
  type: 'GET'
)

@sale_closed: () ->
  @get_server_time(url).done (data) ->
   if not @is_valid_user
     return true
   if Config.override_bid_closed?
    return false
   if data > sale_end
     return true
   return false

Same code in 相同的代码

Javascript: 使用Javascript:

function get_server_time (url) {
  return $.ajax({
    url: url + '/servertime',
    type: 'GET'
  });
};

function sale_closed() {
    get_server_time(url).done(function(data) {
     if (!this.is_valid_user()) {
      return true;
    }
    if (Config.override_sale_closed != null) {
      return false;
    }
    if (data > sale_end) {
      return true;
    }
    return false;
  });
};

Is this correct? 这个对吗?

Is the expectation to change all my sale_close code from 是期望从更改所有我的sale_close代码

foo: () ->
  if sale_closed()
    /* do something */

to

do_something: () ->
 /* do something */

foo: () - >
  sale_closed(do_something)

That's a giant nightmare because I have to change a lot of code and would make them all asynchronous which may break all other functions that call 'foo', 'foo2', etc. (which were synchronous before). 这是一个巨大的噩梦,因为我必须更改很多代码,并使它们全部异步,这可能会破坏调用“ foo”,“ foo2”等(之前是同步的)的所有其他函数。

Is there anything guarantee the variables do_something read will be in context and have the same value when it execute? 是否可以保证变量do_something会在上下文中执行,并且在执行时具有相同的值?

Thanks for your help as I try to wrap my head around this 感谢您的帮助,因为我尝试着解决这个问题


Try doing some logging and if the problem is that you don't have access to the function @is_valid_user then try changing this 尝试进行一些日志记录,如果问题是您无权访问@is_valid_user函数,则尝试更改此记录

  @get_server_time(url).done (data) ->

to this 对此

  @get_server_time(url).done (data) =>

the fat arrow binds the callback function on done to the current context of this therefore preserving the ability to call this.is_valid_user . this.is_valid_user箭头将完成的回调函数绑定this函数的当前上下文,因此保留了调用this.is_valid_user的能力。 Hard to tell exactly if this will work for you without a larger code context, but I might try that first. 很难确切地知道在没有较大代码上下文的情况下这是否对您有用,但是我可能会首先尝试。

Is this correct? 这个对吗?

Not really. 并不是的。 You cannot return from asynchronous callbacks as like as you would in synchronous code. 您不能像在同步代码中那样从异步回调中return Instead, you will want to use Promises: 相反,您将要使用Promises:

@get_server_time: (url) ->
  $.ajax(
    url: url + '/servertime'
    type: 'GET'
  )

@sale_closed: () ->
  if not @is_valid_user
    return $.when true
  if Config.override_bid_closed?
    return $.when false
  @get_server_time(url).then (data) ->
    data > sale_end

Is the expectation to change all my code that calls sale_close? 期望更改我所有调用sale_close的代码吗?

Yes, you will need to change everything to callback style. 是的,您需要将所有内容更改为回调样式。 With promises, the changes won't be very large though, and the flow stays linear. 有了promise,更改不会很大,并且流程保持线性。

Is there anything guarantee the variables do_something read will be in context and have the same value when it execute? 是否可以保证变量do_something会在上下文中执行,并且在执行时具有相同的值?

You should declare do_something locally inside the foo function (or just use a function expression), which will guarantee that all the variables you need are in scope. 您应该在foo函数内部局部声明do_something (或仅使用函数表达式),这将确保所需的所有变量都在范围内。 If you need the right this context, use fat arrow functions. 如果需要this上下文,请使用粗箭头功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM