f97e946ad42c47ac2199cc9f367382265c2b4f5c
[aai/esr-gui.git] /
1 import {
2   invokeCallback,
3   subscribe,
4   FULFILLED,
5   REJECTED,
6   noop,
7   makePromise,
8   PROMISE_ID
9 } from './-internal';
10
11 import { asap } from './asap';
12
13 export default function then(onFulfillment, onRejection) {
14   var parent = this;
15
16   var child = new this.constructor(noop);
17
18   if (child[PROMISE_ID] === undefined) {
19     makePromise(child);
20   }
21
22   var state = parent._state;
23
24   if (state) {
25     var callback = arguments[state - 1];
26     asap(function(){
27       invokeCallback(state, child, callback, parent._result);
28     });
29   } else {
30     subscribe(parent, child, onFulfillment, onRejection);
31   }
32
33   return child;
34 }