如何取消与蓝鸟的承诺

how to cancel promises with bluebird

我想我误解了 bluebird 的 promise 取消是如何工作的。我写了一个测试来证明这一点。我怎样才能让它变绿?感谢:

describe('cancellation tests', () => {
  function fakeFetch() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(1);
      }, 100);
    });
  }

  function awaitAndAddOne(p1) {
    return p1.then(res => res + 1);
  }

  it('`cancel` cancels promises higher up the chain', () => {
    const p1 = fakeFetch();
    const p2 = awaitAndAddOne(p1);

    expect(p2.isCancellable()).toBeTruthy();
    p2.cancel();

    return p2
      .then(() => {
        console.error('then');
      })
      .catch(err => {
        console.error(err);
      })
      .finally(() => {
        expect(p2.isCancelled()).toBeTruthy(); // Expected value to be truthy, instead received false
        expect(p1.isCancelled()).toBeTruthy(); 
      });
  });
});

来自here

The cancellation feature is by default turned off, you can enable it using Promise.config.

似乎您没有在 Promise 本身上启用 cancellation 标志:

Promise.config({
  cancellation: true
});

describe(...

@Karen 如果正确的话。但问题是你的测试也有点不对

如果你看看isCancellable方法

Promise.prototype.isCancellable = function() {
    return this.isPending() && !this.isCancelled();
};

这只是检查 promise 是否为 pending 并且尚未取消。这并不意味着取消启用。

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

如果你看到上面的url,它会在下面引用

The cancellation feature is by default turned off, you can enable it using Promise.config.

如果您查看 cancel 方法

Promise.prototype["break"] = Promise.prototype.cancel = function() {
    if (!debug.cancellation()) return this._warn("cancellation is disabled");

现在,如果我像下面这样正确更新你的测试

var Promise = require("bluebird");
var expect = require("expect");

describe('cancellation tests', () => {
    function fakeFetch() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(1);
        }, 100);
      });
    }

    function awaitAndAddOne(p1) {
      return p1.then(res => res + 1);
    }

    it('`cancel` cancels promises higher up the chain', () => {
      const p1 = fakeFetch();
      const p2 = awaitAndAddOne(p1);

      value = p2.isCancellable();
      expect(p2.isCancellable()).toBeTruthy();
      p2.cancel();

      expect(p2.isCancelled()).toBeTruthy(); // Expected value to be truthy, instead received false
      expect(p1.isCancelled()).toBeTruthy(); 
    });
  });

可以看到取消没有开启,执行警告码

执行结果如期失败

spec.js:46
  cancellation tests
spec.js:46
    1) `cancel` cancels promises higher up the chain
spec.js:78
  0 passing (3m)
base.js:354
  1 failing
base.js:370
  1) cancellation tests
base.js:257
       `cancel` cancels promises higher up the chain:
     Error: expect(received).toBeTruthy()
Expected value to be truthy, instead received
  false
      at Context.it (test/index.test.js:37:36)

现在,如果您更新代码以启用取消功能

var Promise = require("bluebird");
var expect = require("expect");

Promise.config({
  cancellation: true
});

describe('cancellation tests', () => {
    function fakeFetch() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(1);
        }, 100);
      });
    }

    function awaitAndAddOne(p1) {
      return p1.then(res => res + 1);
    }

    it('`cancel` cancels promises higher up the chain', () => {
      const p1 = fakeFetch();
      const p2 = awaitAndAddOne(p1);

      value = p2.isCancellable();
      expect(p2.isCancellable()).toBeTruthy();
      p2.cancel();

      expect(p2.isCancelled()).toBeTruthy(); // Expected value to be truthy, instead received false
      expect(p1.isCancelled()).toBeTruthy(); 
    });
  });

有效!