Home  >  Article  >  Web Front-end  >  Detailed explanation of the role of jQuery's promise object in JavaScript asynchronous programming_jquery

Detailed explanation of the role of jQuery's promise object in JavaScript asynchronous programming_jquery

WBOY
WBOYOriginal
2016-05-16 15:02:241278browse

Promise, Chinese can be understood as wish, representing the final result of a single operation. A Promise has three states: unfulfilled (unfulfilled), fulfilled (satisfied), and failed (failed). Both fulfilled and failed states can be monitored. A wish can change from an unsatisfied state to a satisfied or failed state. Once a wish is in a satisfied or failed state, its status cannot be changed. This "immutable" feature is very important for a Promise. It can prevent the Promise's state listener from modifying the state of a Promise and causing other listeners to behave abnormally. For example: if a listener that monitors the fulfilled state changes the state of the Promise to failed, then the listener in the failed state will be triggered. If a listener in the failed state sets the state of the Promise to fulfilled, then the listener in the fulfilled state will be triggered. device, which will result in an infinite loop. Another way to understand the characteristics of Promise is to think of Promise as a primitive type variable in JavaScript. This variable can be passed into the called function, but cannot be changed by the calling function.

Each Promise object has a method: then(fulfilledHandler, errorHandler, progressHandler), which is used to monitor the different states of a Promise. fulfilledHandler is used to listen to fulfilled events, errorHandler is used to listen to failed events, and progressHandler is used to listen to progress events. A Promise does not force the implementation of progress state event monitoring (jQuery's Deferred is an implementation of Promise, but does not implement the processing of progress state events).

The return value of fulfilledHandler and errorHandler in the then(...) function is a new Promise object so that the then(...) function can be called in a chain. Each callback function returns a Promise in the fulfilled state under normal circumstances. If the callback function returns an error value, the returned Promise state will become failed.

The role of promise in asynchronous programming

Asynchronous mode is becoming more and more important in web programming. For the mainstream web language Javascript, this mode is not very easy to implement. For this reason, many Javascript libraries (such as jQuery and Dojo) add a so-called It is an abstraction of promise (sometimes also called deferred). Through these libraries, developers can use the promise pattern in actual programming.
With the deepening of Web 2.0 technology, the browser side is bearing more and more computing pressure, so "concurrency" has a positive meaning. For developers, they must not only keep the interaction between the page and the user unaffected, but also coordinate the relationship between the page and asynchronous tasks. This kind of non-linear execution programming requirements presents difficulties in adapting. Putting aside page interaction, we can think of two results that need to be processed for asynchronous calls-successful operation and failure processing. After a successful call, we may need to use the returned result in another Ajax request, which will cause a "function chain" situation. This situation creates programming complications. Take a look at the following code example (based on XMLHttpRequest2):

function searchTwitter(term, onload, onerror) {
 
   var xhr, results, url;
   url = 'http://search.twitter.com/search.json?rpp=100&q=' + term;
   xhr = new XMLHttpRequest();
   xhr.open('GET', url, true);
 
   xhr.onload = function (e) {
     if (this.status === 200) {
       results = JSON.parse(this.responseText);
       onload(results);
     }
   };
 
   xhr.onerror = function (e) {
     onerror(e);
   };
 
   xhr.send();
 }
 
 function handleError(error) {
   /* handle the error */
 }
 
 function concatResults() {
   /* order tweets by date */
 }
 
 function loadTweets() {
   var container = document.getElementById('container');
 
   searchTwitter('#IE10', function (data1) {
     searchTwitter('#IE9', function (data2) {
       /* Reshuffle due to date */
       var totalResults = concatResults(data1.results, data2.results);
       totalResults.forEach(function (tweet) {
         var el = document.createElement('li');
         el.innerText = tweet.text;
         container.appendChild(el);
       });
     }, handleError);
   }, handleError);
 }

The function of the above code is to obtain the content with the hashtag IE10 and IE9 in Twitter and display it on the page. This kind of nested callback function is difficult to understand. Developers need to carefully analyze which code is used for the application's business logic, and which code handles asynchronous function calls. The code structure is fragmented. Error handling is also decomposed. We need to detect the occurrence of errors in various places and handle them accordingly.

To reduce the complexity of asynchronous programming, developers have been looking for easy ways to handle asynchronous operations. One of these processing patterns is called a promise, which represents the result of an operation that may be long-running and does not necessarily have to be complete. Instead of blocking and waiting for a long operation to complete, this pattern returns an object that represents the promised result.

Consider an example where the page code needs to access a third-party API. Network delays may cause longer response times. In this case, using asynchronous programming will not affect the interaction between the entire page and the user. The promise mode usually implements a method called then to register the corresponding callback function when the state changes. For example, the following code example:

searchTwitter(term).then(filterResults).then(displayResults);

promise模式在任何时刻都处于以下三种状态之一:未完成(unfulfilled)、已完成(resolved)和拒绝(rejected)。以CommonJS Promise/A 标准为例,promise对象上的then方法负责添加针对已完成和拒绝状态下的处理函数。then方法会返回另一个promise对象,以便于形成promise管道,这种返回promise对象的方式能够支持开发人员把异步操作串联起来,如then(resolvedHandler, rejectedHandler); 。resolvedHandler 回调函数在promise对象进入完成状态时会触发,并传递结果;rejectedHandler函数会在拒绝状态下调用。

有了promise模式,我们可以重新实现上面的Twitter示例。为了更好的理解实现方法,我们尝试着从零开始构建一个promise模式的框架。首先需要一些对象来存储promise。

var Promise = function () {
    /* initialize promise */
  };

接下来,定义then方法,接受两个参数用于处理完成和拒绝状态。

Promise.prototype.then = function (onResolved, onRejected) {
   /* invoke handlers based upon state transition */
 };

同时还需要两个方法来执行理从未完成到已完成和从未完成到拒绝的状态转变。

Promise.prototype.resolve = function (value) {
   /* move from unfulfilled to resolved */
 };
 
 Promise.prototype.reject = function (error) {
   /* move from unfulfilled to rejected */
 };

现在搭建了一个promise的架子,我们可以继续上面的示例,假设只获取IE10的内容。创建一个方法来发送Ajax请求并将其封装在promise中。这个promise对象分别在xhr.onload和xhr.onerror中指定了完成和拒绝状态的转变过程,请注意searchTwitter函数返回的正是promise对象。然后,在loadTweets中,使用then方法设置完成和拒绝状态对应的回调函数。

function searchTwitter(term) {

  var url, xhr, results, promise;
  url = 'http://search.twitter.com/search.json?rpp=100&q=' + term;
  promise = new Promise();
  xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);

  xhr.onload = function (e) {
    if (this.status === 200) {
      results = JSON.parse(this.responseText);
      promise.resolve(results);
    }
  };

  xhr.onerror = function (e) {
    promise.reject(e);
  };

  xhr.send();
  return promise;
}

function loadTweets() {
  var container = document.getElementById('container');
  searchTwitter('#IE10').then(function (data) {
    data.results.forEach(function (tweet) {
      var el = document.createElement('li');
      el.innerText = tweet.text;
      container.appendChild(el);
    });
  }, handleError);
}

到目前为止,我们可以把promise模式应用于单个Ajax请求,似乎还体现不出promise的优势来。下面来看看多个Ajax请求的并发协作。此时,我们需要另一个方法when来存储准备调用的promise对象。一旦某个promise从未完成状态转化为完成或者拒绝状态,then方法里对应的处理函数就会被调用。when方法在需要等待所有操作都完成的时候至关重要。

Promise.when = function () {
  /* handle promises arguments and queue each */
};

以刚才获取IE10和IE9两块内容的场景为例,我们可以这样来写代码:

var container, promise1, promise2;
container = document.getElementById('container');
promise1 = searchTwitter('#IE10');
promise2 = searchTwitter('#IE9');
Promise.when(promise1, promise2).then(function (data1, data2) {

  /* Reshuffle due to date */
  var totalResults = concatResults(data1.results, data2.results);
  totalResults.forEach(function (tweet) {
    var el = document.createElement('li');
    el.innerText = tweet.text;
    container.appendChild(el);
  });
}, handleError);

分析上面的代码可知,when函数会等待两个promise对象的状态发生变化再做具体的处理。在实际的Promise库中,when函数有很多变种,比如 when.some()、when.all()、when.any()等,读者从函数名字中大概能猜出几分意思来,详细的说明可以参考CommonJS的一个promise实现when.js。

除了CommonJS,其他主流的Javascript框架如jQuery、Dojo等都存在自己的promise实现。开发人员应该好好利用这种模式来降低异步编程的复杂性。我们选取Dojo为例,看一看它的实现有什么异同。

Dojo框架里实现promise模式的对象是Deferred,该对象也有then函数用于处理完成和拒绝状态并支持串联,同时还有resolve和reject,功能如之前所述。下面的代码完成了Twitter的场景:

function searchTwitter(term) {

  var url, xhr, results, def;
  url = 'http://search.twitter.com/search.json?rpp=100&q=' + term;
  def = new dojo.Deferred();
  xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);

  xhr.onload = function (e) {
    if (this.status === 200) {
      results = JSON.parse(this.responseText);
      def.resolve(results);
    }
  };

  xhr.onerror = function (e) {
    def.reject(e);
  };

  xhr.send();
  return def;
}

dojo.ready(function () {
  var container = dojo.byId('container');
  searchTwitter('#IE10').then(function (data) {
    data.results.forEach(function (tweet) {
      dojo.create('li', {
        innerHTML: tweet.text
      }, container);
    });
  });
});

不仅如此,类似dojo.xhrGet方法返回的即是dojo.Deferred对象,所以无须自己包装promise模式。

var deferred = dojo.xhrGet({
  url: "search.json",
  handleAs: "json"
});

deferred.then(function (data) {
  /* handle results */
}, function (error) {
  /* handle error */
});

除此之外,Dojo还引入了dojo.DeferredList,支持开发人员同时处理多个dojo.Deferred对象,这其实就是上面所提到的when方法的另一种表现形式。

dojo.require("dojo.DeferredList");
dojo.ready(function () {
  var container, def1, def2, defs;
  container = dojo.byId('container');
  def1 = searchTwitter('#IE10');
  def2 = searchTwitter('#IE9');

  defs = new dojo.DeferredList([def1, def2]);

  defs.then(function (data) {
    // Handle exceptions
    if (!results[0][0] || !results[1][0]) {
      dojo.create("li", {
        innerHTML: 'an error occurred'
      }, container);
      return;
    }
    var totalResults = concatResults(data[0][1].results, data[1][1].results);

    totalResults.forEach(function (tweet) {
      dojo.create("li", {
        innerHTML: tweet.text
      }, container);
    });
  });
});

上面的代码比较清楚,不再详述。

说到这里,读者可能已经对promise模式有了一个比较完整的了解,异步编程会变得越来越重要,在这种情况下,我们需要找到办法来降低复杂度,promise模式就是一个很好的例子,它的风格比较人性化,而且主流的JS框架提供了自己的实现。所以在编程实践中,开发人员应该尝试这种便捷的编程技巧。需要注意的是,promise模式的使用需要恰当地设置promise对象,在对应的事件中调用状态转换函数,并且在最后返回promise对象。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn