How to Write a Generic findZero Algorithm for Both Sync and Async Functions in JavaScript/TypeScript

Published: 22 December 2025
on channel: vlogommentary
No
like

Learn how to implement a single generic algorithm in JavaScript/TypeScript that works seamlessly with both synchronous and asynchronous user functions using generator functions.
---
This video is based on the question https://stackoverflow.com/q/79461579/ asked by the user 'Niobos' ( https://stackoverflow.com/u/3486486/ ) and on the answer https://stackoverflow.com/a/79461748/ provided by the user 'Bergi' ( https://stackoverflow.com/u/1048572/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How / Is there a way to have a generic algorithm function work with both sync & async functions?

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.

If anything seems off to you, please feel free to drop me a comment under this video.
---
Supporting Both Sync and Async Functions in a Generic Algorithm

When writing generic algorithm functions in JavaScript or TypeScript, such as optimization routines, you'll sometimes want them to work with both synchronous and asynchronous user-supplied functions.

For example, consider a simple algorithm to find the first input x where f(x) === 0. A straightforward sync implementation works well with synchronous f, but fails with async functions because it receives a Promise instead of the resolved value.

[[See Video to Reveal this Text or Code Snippet]]

You can make an async version easily by using await inside the loop:

[[See Video to Reveal this Text or Code Snippet]]

But having two almost identical functions isn't ideal. How can we write one generic algorithm that works with both?



Solution: Use a Generator to Separate Iteration From Evaluation

You can implement your algorithm as a generator function that yields inputs requiring evaluation (f(i)), and receives their results through next() calls. Then, provide two runners:

A synchronous runner that calls the generator and feeds values from a synchronous function

An asynchronous runner that calls the generator and awaits the promise returned by an async function

Step 1: The Generator Algorithm

[[See Video to Reveal this Text or Code Snippet]]

This generator produces values i to test and expects the evaluated result to be passed back with next().

Step 2: Synchronous Runner

[[See Video to Reveal this Text or Code Snippet]]

This runs the generator synchronously by immediately applying f and passing the result back.

Step 3: Asynchronous Runner

[[See Video to Reveal this Text or Code Snippet]]

This runner awaits each promise before passing the resolved value into the generator.



Benefits

Single algorithm logic encapsulated in the generator.

Clear separation of iteration from evaluation.

Flexible integration with sync or async user functions.

Summary

By rewriting your algorithm as a generator function, you can:

Yield evaluation points (inputs)

Receive evaluated results

Run the same algorithm logic both synchronously and asynchronously with minimal duplicated code.

This approach can be generalized easily to more complex iterative algorithms requiring user function calls, preserving code maintainability and flexibility.



Example Usage:

[[See Video to Reveal this Text or Code Snippet]]

This pattern provides a clean and powerful technique to unify synchronous and asynchronous workflows in generic algorithm implementations.


On this page of the site you can watch the video online How to Write a Generic findZero Algorithm for Both Sync and Async Functions in JavaScript/TypeScript with a duration of hours minute second in good quality, which was uploaded by the user vlogommentary 22 December 2025, share the link with friends and acquaintances, this video has already been watched No times on youtube and it was liked by like viewers. Enjoy your viewing!