使 JavaScript 函数与回调异步

Making a javascript function async with callback

本文关键字:回调 异步 函数 JavaScript      更新时间:2023-09-26

以前我有

 MyClass.prototype.method1 = function(data1) {
    return this.data111.push(data1);
  };
 MyClass.prototype.method2 = function(i) {
    var data = this.method1(i);
    if (data.condition1 != null) {
      data.onEvent1(this);
    }
    return $(data.element).someMethod123("data123");
  };
 MyClass.prototype.method3 = function() {
    var data1 = this.method1(this._data1);
    return this.someMethod123(step.data1);
  };
MyClass.prototype.ended = function() {
    return !!this.getState("end");
  };

MyClass.prototype.getState = function(key) {
    var value = $.cookie(key);
    this._options.afterGetState(key, value);
    return value;
  };

如何使用回调函数进行异步?我想应该是这样:

 MyClass.prototype.method1 = function(data1, callback) {
    if(callback){
      callback(this.data111.push(data1));
    }
    else{
      return this.data111.push(data1);
   }
  };
 MyClass.prototype.method2 = function(i, callback) {
    var data = this.method1(i);
    if (data.condition1 != null) {
      data.onEvent1(this);
    }
    if(callback){
      callback($(data.element).someMethod123("data123"));
    }
    else{
     return $(data.element).someMethod123("data123");
    }
  };
 MyClass.prototype.method3 = function(callback) {
    var data1 = this.method1(this._data1);
     if(callback){
      callback(this.someMethod123(step.data1));
    }
    else{
       return this.someMethod123(step.data1);
    }
  };
MyClass.prototype.ended = function(callback) {
    if(callback){
       callback(!!this.getState("end", /*what should be here and what should it does?*/));
    }
  };

  MyClass.prototype.getState = function(key, callback) { 
    var oldThis = this;
    setTimeout(function(){ 
                    value = $.cookie(key);
                    callback(value, oldThis); 
                    oldThis._options.afterGetState(key, value);
                  },
              0);
  };

我肯定错过了一些东西,因为我以前从未在 javascript 中使用过异步函数。所以呢?

而且,据我了解,要使函数异步,我基本上应该再添加一个参数作为回调函数并摆脱返回,不是吗?

只需传递回调:

MyClass.prototype.ended = function(callback) {
    this.getState("end", callback);
};

你也应该在其他函数中这样做,我建议坚持使用一个界面。 即直接返回值(如果可能)或使用回调。

只有那些执行异步任务的方法需要回调样式。没有理由将其用于method1method2method3

现在getState是实际的异步方法。使用 ajax/setTimeout/这里很明显的任何内容。然而,我可以发现一个错误:callback调用应该始终是最后一个语句,就像你在return语句之后不会做任何事情一样。即使可以,最好设置内部选项对象后回调:

                …
                oldThis._options.afterGetState(key, value);
                callback(value, oldThis);

现在,ended方法。由于它使用异步getState,它本身将变得异步,您需要使用回调样式(请注意,getState()不会返回该值)。所以你会调用getState,当回调时,你将转换结果并将其传递给你自己的回调:

MyClass.prototype.ended = function(callback) {
    this.getState("end", function ownCallback(state) {
        var result = !!state; // or whatever you need to do
        callback(result);
    });
};
// and if you don't need to do anything with the result, you can leave out ownCallback:
MyClass.prototype.ended = function(callback) {
    this.getState("end", callback);
};