HTTP.Promise (http_fetch v0.5.0)

Represents an asynchronous HTTP operation, similar to a JavaScript Promise. It wraps an underlying Task and provides an await function.

Summary

Functions

Awaits the completion of the HTTP promise.

Attaches callbacks for the resolution or rejection of the Promise. Returns a new HTTP.Promise for chaining.

Types

error_callback_fun()

@type error_callback_fun() :: (term() -> any())

success_callback_fun()

@type success_callback_fun() :: (HTTP.Response.t() -> any())

t()

@type t() :: %HTTP.Promise{task: Task.t()}

Functions

await(promise, timeout \\ :infinity)

@spec await(t(), timeout :: non_neg_integer() | :infinity) ::
  HTTP.Response.t() | {:error, term()}

Awaits the completion of the HTTP promise.

Arguments:

  • promise: The HTTP.Promise instance to await.
  • timeout: Optional timeout in milliseconds or :infinity. Defaults to :infinity.

Returns:

  • %HTTP.Response{} on successful completion.
  • {:error, reason} if the request fails or is aborted.
  • {:error, :timeout} if the timeout is reached.

then(promise, success_fun, error_fun \\ nil)

@spec then(t(), success_callback_fun(), error_callback_fun() | nil) :: t()

Attaches callbacks for the resolution or rejection of the Promise. Returns a new HTTP.Promise for chaining.

Arguments:

  • promise: The current HTTP.Promise instance.
  • success_fun: A 1-arity function to be called if the promise resolves successfully.
               It receives the `HTTP.Response.t()` as an argument.
               Can return a value, `{:ok, value}`, `{:error, reason}`, or another `HTTP.Promise`.
  • error_fun: An optional 1-arity function to be called if the promise is rejected.
             It receives the reason for rejection as an argument.
             Can return a value, `{:ok, value}`, `{:error, reason}`, or another `HTTP.Promise`.

Returns:

  • %HTTP.Promise{}: A new promise representing the outcome of the callbacks.