Skip to content

[wish] function to construct a promise that is already fulfilled #104

Open
@mbacarella

Description

@mbacarella

If the task is small enough you may not want to dispatch it to the thread pool.

In those cases it would be nice if you could simply create a promise that's already fulfilled, so that code that runs await on it can work in either case.

Supposing we can have a Task.return that creates a fulfilled promise, here's a contrived example:

let work, worklen = get_work () in
let changes =
  (* skip dispatching to pool if workload is small *)
  if worklen < 3
  then List.map work ~f:(fun x -> Task.return pool (thunk_opt x))
  else List.map work ~f:(fun x -> Task.async pool (fun () -> thunk_opt x)))
in
List.fold_left changes ~init:[] ~f:(fun acc res ->
  match Task.await pool res with
  | None -> acc
  | Some y -> y :: acc)

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