
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
async-off
: make asynchronous TypeScript code synchronousUsing without installation:
npx async-off path/to/file.ts
Installation:
npm install -g async-off
async-off path/to/file.ts
cat path/to/file.ts | async-off
async-off --help
async-off -h
async-off
converts asynchronous TypeScript code to synchronous code by:
await
in expressions and in for-await-of
loops.async
.Promise<X>
to X
.AsyncIterable<X>
to Iterable<X>
and AsyncIterator<X>
to Iterator<X>
.More changes can be configured (see below).
async-off
’s approach only works for very simple code. Then you can avoid writing very similar code twice – once asynchronously and once synchronously. Examples of such code include:
map()
, filter()
, etc. (see next section)Input:
async function* map<In, Out>(
mapperFn: (x: In) => Out,
iterable: AsyncOrSyncIterable<In>
): AsyncIterable<Out> {
for await (const x of iterable) {
yield mapperFn(x);
}
}
//<async>
type AsyncOrSyncIterable<Item> = AsyncIterable<Item> | Iterable<Item>;
//</async>
//<async-off-config>
// {
// "renameType": {
// "AsyncOrSyncIterable": "Iterable"
// }
// }
//</async-off-config>
Output:
function* map<In, Out>(
mapperFn: (x: In) => Out,
iterable: Iterable<In>
): Iterable<Out> {
for (const x of iterable) {
yield mapperFn(x);
}
}
async-off
is configured via embedded JSON (see previous section). The following properties are supported:
interface ConfigJson {
/**
* Removes a function call wrapped around a value.
* Use case – unit testing:
*
* ```ts
* processAsyncData(toAsyncIterable(['a', 'b', 'c']));
* processSyncData(['a', 'b', 'c']);
* ```
*
* Note: (processAsyncData() is renamed separately.)
*/
unwrapFunctionCall?: Array<string>;
/**
* Included by default: `"Promise"`
*/
unwrapParamerizedType?: Array<string>;
renameVariable?: Record<string, string>;
/**
* Included by default: `{"AsyncIterable": "Iterable", "AsyncIterator": "Iterator"}`
*/
renameType?: Record<string, string>;
}
const Functions = {
map,
filter,
};
//<async>
export {Functions as AsyncIterable};
//</async>
//<sync>
// export {Functions as Iterable};
//</sync>
async-off
uses the library ts-morph
FAQs
Make asynchronous TypeScript code synchronous
We found that async-off demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.