forio / Curl.jl

a Julia HTTP curl library
Other
11 stars 8 forks source link

Obligatory Promises Request #6

Open adambom opened 11 years ago

adambom commented 11 years ago

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 commented 11 years ago

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.