Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.
This repository was archived by the owner on Nov 19, 2025. It is now read-only.

Obligatory Promises Request #6

Description

@adambom

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions