All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.englishtown.promises.internal.AssimilateTask Maven / Gradle / Ivy

The newest version!
package com.englishtown.promises.internal;

import com.englishtown.promises.Promise;
import com.englishtown.promises.Thenable;
import com.englishtown.promises.internal.handlers.Handler;

import java.util.function.Function;

/**
 * Assimilate a thenable, sending it's value to resolver
 */
public class AssimilateTask implements Runnable {

    private final Thenable thenable;
    private final Handler resolver;

    public AssimilateTask(Thenable thenable, Handler resolver) {
        this.thenable = thenable;
        this.resolver = resolver;
    }

    @Override
    public void run() {
        Handler h = this.resolver;

        Function> _resolve = (x) -> {
            h.resolve(x);
            return null;
        };

        Function> _reject = (x) -> {
            h.reject(x);
            return null;
        };

        tryAssimilate(this.thenable, _resolve, _reject);

    }

    private void tryAssimilate(
            Thenable thenable,
            Function> resolve,
            Function> reject) {

        try {
            thenable.then(resolve, reject);
        } catch (Throwable e) {
            reject.apply(e);
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy