Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mbacarella opened this issue Jan 16, 2023 · 0 comments
Open

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

mbacarella opened this issue Jan 16, 2023 · 0 comments

Comments

@mbacarella
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant