bluebird中的.cancel方法

http://bluebirdjs.com/docs/api/cancellation.html

var searchPromise = Promise.resolve();  // Dummy promise to avoid null check.
document.querySelector("#search-input").addEventListener("input", function() {
    // The handlers of the previous request must not be called
    searchPromise.cancel();
    var url = "/search?term=" + encodeURIComponent(this.value.trim());
    showSpinner();
    searchPromise = makeCancellableRequest(url)
        .then(function(results) {
            return transformData(results);
        })
        .then(function(transformedData) {
            document.querySelector("#search-results").innerHTML = transformedData;
        })
        .catch(function(e) {
            document.querySelector("#search-results").innerHTML = renderErrorBox(e);
        })
        .finally(function() {
            // This check is necessary because `.finally` handlers are always called.
            if (!searchPromise.isCancelled()) {
                hideSpinner();
            }
        });
});

bluebird的文档是说,调用cancel方法后,前面的请求都不会执行,我按照例子来,还是执行了。

var Promise = require('bluebird');
var a = require('./a');
var b = require('./b');

var cancelPromise = Promise.resolve();
cancelPromise.cancel();
cancelPromise = a.fnA()
  .then(function() {
    return b.fnB();
  })
  .then(function() {
    console.log('done');
  })
  .finally(function() {
    if (cancelPromise.isCancelled()) {
      console.log('canceled');
    }
    console.log('end');
  });

姿势不对?

阅读 5.3k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏