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

feat(ops): 'hybrid' ops - sync returning future #14640

Merged
merged 2 commits into from
Jun 8, 2022

Conversation

littledivy
Copy link
Member

Support for 'hybrid' (sync + async) ops. Ops can have a sync phase to do things, like creating V8 handles and using values that aren't movable to the async block.

#[op]
fn op_sum_async(nums: Vec<f64>) -> impl Future<Output = f64> + 'static {
  // Sum inputs
  let sum = nums.iter().fold(0.0, |a, v| a + v);
  async move { sum }
}

@littledivy littledivy marked this pull request as ready for review May 17, 2022 08:16
@littledivy littledivy requested a review from AaronO as a code owner May 17, 2022 08:16
@aapoalas
Copy link
Collaborator

One thing that this reminds me is the pain of these sorts of JS functions:

function hybrid_func(nums) {
  const stringifiedNumbers = nums.toString(); // Throws if nums happens to be null
  return fetch(getSomeAddress(stringifiedNumbers));
}

hybrid_func(null).catch(err => {}); // .catch() won't work because the function throws synchronously

To put this in context with this PR: The only way these hybrid functions can do synchronous fallible work is by using .unwrap(). If these ops are intended to be equally powerful, then the return value should support something like Result<impl Future<Output = T> + 'static, Error> if that is possible. This will make the ops macro quite a bit more complicated.

@bartlomieju
Copy link
Member

@littledivy what is the use case for this PR? AFAIK ops are already eagerly polled, so you should be able to run some code in the same tick of the event loop, until await point is encountered.

@littledivy littledivy merged commit 753f320 into denoland:main Jun 8, 2022
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

Successfully merging this pull request may close these issues.

None yet

3 participants