I have released a library called sync-actions that allows asynchronous processes to be executed synchronously in JavaScript/TypeScript. Especially in TypeScript, you can call defined functions in a type-safe manner. It is intended for use in cases where you want to execute asynchronous processes within functions that you do not want (or cannot) mark as async.
https://github.com/koyopro/sync-actions
It is published as an npm package, so please install it using npm install or similar.
npm install sync-actions
By passing an asynchronous function that returns a Promise object to defineSyncWorker(), you can define the interface and start the worker thread with launch(). It is assumed that the file defining the worker is created separately from other processing files.
// worker.js import { defineSyncWorker } from "sync-actions"; export const { actions, worker } = defineSyncWorker(import.meta.filename, { ping: async () => { // Execute asynchronous process, await new Promise((resolve) => setTimeout(resolve, 1000)); // Return the result as a return value return "pong"; } }).launch();
// main.js import { actions, worker } from "./worker.js"; // You can execute asynchronous functions synchronously console.log(actions.ping()); // => "pong" is output after 1 second worker.terminate();
In TypeScript, you can call functions defined with defineSyncWorker in a type-safe manner.
// worker.ts import { defineSyncWorker } from "sync-actions"; export const { actions, worker } = defineSyncWorker(import.meta.filename, { // By specifying the types of arguments and return values, type-safe calls are possible add: async (a: number, b: number): Promise<number> => { return a + b; } }).launch();
// main.ts import { actions, worker } from "./worker.js"; // Type-safe call actions.add(1, 2); // => 3 (number) // @ts-expect-error actions.add("1", 2); // => Argument of type 'string' is not assignable to parameter of type 'number' worker.terminate();
The content so far is the same as the README, so I will describe the background of its creation.
I am developing an ORM called Accel Record.1 Unlike general ORMs, Accel Record is designed to perform DB access with a synchronous interface.2 The part that executes DB access synchronously was realized by executing asynchronous processes in a subprocess started with the child_process module.3 I thought that by using worker_threads instead of child_process, I could reduce the overhead at runtime.
Accel Record is also designed to be similar to Ruby on Rails' Active Record in terms of usability, and one of the things I want to achieve in the future is to create a library like CarrierWave. CarrierWave allows you to save images to external storage services (such as AWS S3) when saving records, and to achieve this with Accel Record, it is necessary to execute asynchronous processes such as image uploads synchronously. I expect that this process can be executed faster by using worker_threads instead of subprocesses.
So I once looked for a library that synchronously executes asynchronous processes using worker_threads. I found several libraries such as synckit and deasync, but none of them worked as expected on my end, so I decided to create my own. Since I was at it, I thought I would make an interface that can be used in a type-safe manner with TypeScript.
Introduction to "Accel Record": A TypeScript ORM Using the Active Record Pattern ↩
Why We Adopted a Synchronous API for the New TypeScript ORM ↩
Techniques for Synchronous DB Access in TypeScript ↩
The above is the detailed content of Released a Library for Synchronous Execution of Asynchronous Processes in JS/TS. For more information, please follow other related articles on the PHP Chinese website!