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

Obligatory Promises Request #6

Open
adambom opened this issue Feb 27, 2013 · 1 comment
Open

Obligatory Promises Request #6

adambom opened this issue Feb 27, 2013 · 1 comment

Comments

@adambom
Copy link

adambom commented Feb 27, 2013

Since promises are apparently cool right now, I feel like I should raise this issue.

We shouldn't really have blocking HTTP requests. Can we have the verbs return a promise object, such that I can do:

Curl.get("http://jsonip.com").then((res) -> println(res.text))

Then would take three arguments: onResolved, onError, and onProgress.

I would express it like this:

type Promise
    then::Function
    resolved_callbacks::Vector
    rejected_callbacks::Vector
    progress_callbacks::Vector
    resolve::Function
    reject::Function
    progress::Function

    function then(on_resolved, on_error, on_progress)
        # push callbacks into queues
    end

    function resolve(value)
        # if no handlers left return
        # shift handler off queue
        # invoke handler with value
        # recurse
    end

    function reject(error)
        # if no handlers left return
        # shift handler off queue
        # invoke handler with value
        # recurse
    end

    function progress(value)
        # if no handlers left return
        # shift handler off queue
        # invoke handler with value
        # recurse
    end

    function Promise()
        new(then, [], [], [], resolve, reject, progress)
    end

end

get = function (url)
    promise = Promise()

    on_complete(data) = promise.resolve
    on_error(error) = promise.reject
    on_progress(data) = promise.progress

    do_curl() # with appropriate arguments

    promise;
end

Of course that doesn't allow us to chain then calls, but in general I find chaining to be very difficult in Julia. There may be a way to do it. We could also just pass in handlers as a vector of functions instead of just one function.

Open to other approaches too.

@WestleyArgentum
Copy link
Contributor

At least right now I'm of the opinion that we should just return RemotRefs to response objects.

This seems to give the most flexibility in terms of blocking when needed but also firing off tons of requests (potentially utilizing added processors) before waiting for responses.

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

2 participants