Search in sources :

Example 1 with ZoneObject

use of com.github.anba.es6draft.runtime.objects.zone.ZoneObject in project es6draft by anba.

the class PromisePrototype method PerformPromiseThen.

/**
 * 25.4.5.3.1 PerformPromiseThen ( promise, onFulfilled, onRejected, resultCapability )
 *
 * @param <PROMISE>
 *            the promise type
 * @param cx
 *            the execution context
 * @param promise
 *            the promise object
 * @param onFulfilled
 *            the onFulfilled handler
 * @param onRejected
 *            the onRejected handler
 * @param resultCapability
 *            the new promise capability record
 * @return the new promise object
 */
public static <PROMISE extends ScriptObject> PROMISE PerformPromiseThen(ExecutionContext cx, PromiseObject promise, Object onFulfilled, Object onRejected, PromiseCapability<PROMISE> resultCapability) {
    /* step 3 */
    if (!IsCallable(onFulfilled)) {
        onFulfilled = null;
    }
    /* step 4 */
    if (!IsCallable(onRejected)) {
        onRejected = null;
    }
    ZoneObject currentZone = cx.getRealm().getCurrentZone();
    /* step 5 */
    PromiseReaction fulfillReaction = new PromiseReaction(resultCapability, PromiseReaction.Type.Fulfill, (Callable) onFulfilled, currentZone);
    /* step 6 */
    PromiseReaction rejectReaction = new PromiseReaction(resultCapability, PromiseReaction.Type.Reject, (Callable) onRejected, currentZone);
    /* step 7 */
    if (promise.getState() == PromiseObject.State.Pending) {
        promise.addFulfillReaction(fulfillReaction);
        promise.addRejectReaction(rejectReaction);
        promise.notifyRejectReaction(rejectReaction);
    } else /* step 8 */
    if (promise.getState() == PromiseObject.State.Fulfilled) {
        Object value = promise.getResult();
        Realm realm = cx.getRealm();
        realm.enqueuePromiseJob(new PromiseReactionJob(realm, fulfillReaction, value));
    } else /* step 9 */
    {
        assert promise.getState() == PromiseObject.State.Rejected;
        Object reason = promise.getResult();
        Realm realm = cx.getRealm();
        realm.enqueuePromiseJob(new PromiseReactionJob(realm, rejectReaction, reason));
        promise.notifyRejectReaction(rejectReaction);
    }
    /* step 11 */
    return resultCapability.getPromise();
}
Also used : ZoneObject(com.github.anba.es6draft.runtime.objects.zone.ZoneObject) PromiseReactionJob(com.github.anba.es6draft.runtime.objects.promise.PromiseAbstractOperations.PromiseReactionJob) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ZoneObject(com.github.anba.es6draft.runtime.objects.zone.ZoneObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

Realm (com.github.anba.es6draft.runtime.Realm)1 PromiseReactionJob (com.github.anba.es6draft.runtime.objects.promise.PromiseAbstractOperations.PromiseReactionJob)1 ZoneObject (com.github.anba.es6draft.runtime.objects.zone.ZoneObject)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1