then.js 609 B

123456789101112131415161718192021222324252627282930313233
  1. import {
  2. invokeCallback,
  3. subscribe,
  4. FULFILLED,
  5. REJECTED,
  6. noop,
  7. makePromise,
  8. PROMISE_ID
  9. } from './-internal';
  10. import { asap } from './asap';
  11. export default function then(onFulfillment, onRejection) {
  12. const parent = this;
  13. const child = new this.constructor(noop);
  14. if (child[PROMISE_ID] === undefined) {
  15. makePromise(child);
  16. }
  17. const { _state } = parent;
  18. if (_state) {
  19. const callback = arguments[_state - 1];
  20. asap(() => invokeCallback(_state, child, callback, parent._result));
  21. } else {
  22. subscribe(parent, child, onFulfillment, onRejection);
  23. }
  24. return child;
  25. }