package promise_jsoo

  1. Overview
  2. Docs

Module PromiseSource

Sourcetype +'a t = private < > Js_of_ocaml.Js.t

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

Sourcetype error = private Js_of_ocaml.Js.Unsafe.any

Type for errors returned by reject

Sourceval make : (resolve:('a -> unit) -> reject:('e -> unit) -> unit) -> 'a t

Creates a new Promise object. The constructor is primarily used to wrap functions that do not already support promises.

Instance Methods

Sourceval catch : rejected:(error -> 'a t) -> 'a t -> 'a t

Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.

Sourceval then_ : fulfilled:('a -> 'b t) -> ?rejected:(error -> 'b t) -> 'a t -> 'b t

Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler, or to its original settled value if the promise was not handled (i.e. if the relevant handler fulfilled or rejected is not a function).

Sourceval finally : f:(unit -> unit) -> 'a t -> 'a t

Appends a handler to the promise, and returns a new promise that is resolved when the original promise is resolved. The handler is called when the promise is settled, whether fulfilled or rejected.

Static Methods

Sourceval all : 'a t array -> 'a array t

Wait for all promises to be resolved, or for any to be rejected.

If the returned promise resolves, it is resolved with an aggregating array of the values from the resolved promises ,in the same order as defined in the iterable of multiple promises.

If it rejects, it is rejected with the reason from the first promise in the iterable that was rejected.

Sourceval all2 : ('a t * 'b t) -> ('a * 'b) t

Specialization of all for a tuple of 2 promises

Sourceval all3 : ('a t * 'b t * 'c t) -> ('a * 'b * 'c) t

Specialization of all for a tuple of 3 promises

Sourceval all_list : 'a t list -> 'a list t

Specialization of all for a list of promises

Sourceval race : 'a t array -> 'a t

Wait until any of the promises is resolved or rejected.

If the returned promise resolves, it is resolved with the value of the first promise in the iterable that resolved.

If it rejects, it is rejected with the reason from the first promise that was rejected.

Sourceval race_list : 'a t list -> 'a t

Specialization of race for a list of promises

Sourceval reject : 'e -> 'a t

Returns a new Promise object that is rejected with the given reason.

Sourceval resolve : 'a -> 'a t

Returns a new Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.

Generally, if you don't know if a value is a promise or not, Promise.resolve value it instead and work with the return value as a promise.

Supplemental API

Functions that are not part of the original JS Promise API, but are useful for writing OCaml code

Sourceval return : 'a -> 'a t

Equivalent to resolve

Sourceval map : ('a -> 'b) -> 'a t -> 'b t
Sourceval bind : ('a -> 'b t) -> 'a t -> 'b t
Sourcemodule Syntax : sig ... end
Sourcemodule Option : sig ... end
Sourcemodule Result : sig ... end
Sourcemodule Array : sig ... end
Sourcemodule List : sig ... end

Compatibility with gen_js_api

Sourceval t_to_js : ('a -> Ojs.t) -> 'a t -> Ojs.t
Sourceval t_of_js : (Ojs.t -> 'a) -> Ojs.t -> 'a t
Sourcetype void = unit t
Sourceval void_to_js : void -> Ojs.t
Sourceval void_of_js : Ojs.t -> void
Sourceval error_to_js : error -> Ojs.t
Sourceval error_of_js : Ojs.t -> error
OCaml

Innovation. Community. Security.