_Promise

说明

模拟实现 ES6Promise

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/algorithm/es6-achieve/promise

用法

_Promise.test.ts
describe('ES6Achieve._Promise tests', () => {
  const _Promise = ES6Achieve._Promise;

  test('_Promise should work fine', () => {
    const result1 = new _Promise((resolve) => {
      setTimeout(() => {
        resolve(1);
      }, 0);
    });
    result1.then((value) => {
      expect(value).toBe(1);

      return value + 1;
    }).then((value) => {
      expect(value).toBe(2);
    });

    const result2 = new _Promise((resolve) => {
      resolve(100);
    });
    result2.then((value) => {
      expect(value).toBe(100);

      return value / 5;
    }).then((value) => {
      expect(value).toBe(20);
    });
  });
});

最后更新于