Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add promise.with #5

Open
jakub-bochenski opened this issue Mar 25, 2016 · 3 comments
Open

Feature request: add promise.with #5

jakub-bochenski opened this issue Mar 25, 2016 · 3 comments

Comments

@jakub-bochenski
Copy link

I'd like to be able to pass some context object (e.g. a pre-initializer logger) along the promise chain.
Bluebird has a bind() method that does that.

I think implementing this would be rather simple, the question is how to make it work with Java syntax.

Closure-scope is not adequate here, as I want the same logger to be shared end-to-end and not only in the steps defined in current method.

@naugtur
Copy link

naugtur commented Mar 29, 2016

Since this is a port of when.js the method in question should be this:
https://github.com/cujojs/when/blob/master/docs/api.md#promisewith

@jakub-bochenski jakub-bochenski changed the title Feature request: add Bluebird's Promise.bind Feature request: add promise.with Mar 29, 2016
@adrianluisgonzalez
Copy link
Member

Thanks for the suggestion @naugtur, I haven't looked at when.js in quite a while.

Do either of you want to take a stab at adding Promise.with(thisArg)?

@jakub-bochenski
Copy link
Author

I'd be willing to try and implement this, but I'm not sure how to handle the fact that Java doesn't seem to have anything analogous to Function.apply() in JS

My first attempt to work around this involves adding methods accepting a BiFunction<T,C, ...>in addition to regular Function<T, ... >, I'm not sure if this is adequate though.

interface ContextualThenable<T, C>
        extends Thenable<T> {

    default <U> Thenable<U> then(BiFunction<T, C, ? extends Thenable<U>> onFulfilled) {
        return then(onFulfilled, null);
    }

    <U> Thenable<U> then(BiFunction<T, C, ? extends Thenable<U>> onFulfilled,
            BiFunction<Throwable, C, ? extends Thenable<U>> onRejected);

    // unbinds context
    Thenable<T> with();
}

in simplest case you'd bind the context using this method:

interface Thenable<T> {
 //...
    <C> ContextualThenable<T, C> with(C thisArg);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants