{"id":23796924,"url":"https://github.com/kaciras/deasync","last_synced_at":"2025-05-16T05:07:31.935Z","repository":{"id":37619792,"uuid":"460339563","full_name":"Kaciras/deasync","owner":"Kaciras","description":"Turns async function into sync via JavaScript wrapper of Node event loop","archived":false,"fork":false,"pushed_at":"2025-03-01T01:40:02.000Z","size":561,"stargazers_count":51,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-12T08:37:45.473Z","etag":null,"topics":["async","nodejs","promise","sync"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"abbr/deasync","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kaciras.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-02-17T08:10:39.000Z","updated_at":"2025-05-06T16:10:52.000Z","dependencies_parsed_at":"2023-12-31T02:53:00.741Z","dependency_job_id":"78a3b493-2ed6-4e6f-9f06-5a764de18be3","html_url":"https://github.com/Kaciras/deasync","commit_stats":{"total_commits":232,"total_committers":17,"mean_commits":"13.647058823529411","dds":"0.43534482758620685","last_synced_commit":"509715ad16e8d5c3b4b1b884af0b4599a8d9b654"},"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kaciras%2Fdeasync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kaciras%2Fdeasync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kaciras%2Fdeasync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kaciras%2Fdeasync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kaciras","download_url":"https://codeload.github.com/Kaciras/deasync/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254471060,"owners_count":22076585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","nodejs","promise","sync"],"created_at":"2025-01-01T20:13:41.466Z","updated_at":"2025-05-16T05:07:31.914Z","avatar_url":"https://github.com/Kaciras.png","language":"TypeScript","readme":"# DeAsync\n\n[![Npm Version](https://img.shields.io/npm/v/@kaciras/deasync)](https://www.npmjs.com/package/@kaciras/deasync)\n![NPM Type Definitions](https://img.shields.io/npm/types/%40kaciras%2Fdeasync)\n![node-current (scoped)](https://img.shields.io/node/v/@kaciras/deasync)\n[![Test](https://github.com/Kaciras/deasync/actions/workflows/test.yml/badge.svg)](https://github.com/Kaciras/deasync/actions/workflows/test.yml)\n[![codecov](https://codecov.io/gh/Kaciras/deasync/branch/master/graph/badge.svg?token=ST7ROWQH0Z)](https://codecov.io/gh/Kaciras/deasync)\n![Static Badge](https://img.shields.io/badge/dependencies-0-46c018)\n\nDeAsync turns async code into sync, implemented with a blocking mechanism by calling Node.js event loop at JavaScript layer. The core of deasync is written in C++.\n\nThis project is forked from [abbr/deasync](https://github.com/abbr/deasync) and rewritten in modern code, **adding some new features: types, Promise support, and prebuild binaries.**\n\nThe benefit of this package over [synckit](https://github.com/un-ts/synckit), [await-sync](https://github.com/jimmywarting/await-sync) and others libs is that this runs your code in the current context, so parameters and the return value of your function are no need to be serializable, you are free to use `Symbol`, functions, and objects with prototypes.\n\n\u003e [!WARNING]\n\u003e \n\u003e Due to [`uv_run()` is not reentrant](https://docs.libuv.org/en/v1.x/loop.html#c.uv_run), functions that poll the event loop and deasynced functions only work at the top level, and calling them from asynchronous callbacks can lead to deadlocks.\n\n## Installation\n\n```shell\nnpm install @kaciras/deasync\n```\n\nDeAsync downloads prebuild binary from GitHub releases during installation, if download fails, try to build locally. You can skip the install phase by setting the environment variable `NO_PREBUILD=1`.\n\nDeAsync uses node-gyp to compile C++ source code, so to build Deasync you may need the compilers listed in [node-gyp](https://github.com/nodejs/node-gyp).\n\n## Usage\n\n### `deasync(function)`\n\nGeneric wrapper of async function with conventional API signature `function(...args, (error, result) =\u003e {})`. Returns `result` and throws `error` as exception if not null.\n\nSleep (a wrapper of setTimeout):\n\n```javascript\nconst { deasync } = require(\"@kaciras/deasync\");\n\nconst sleep = deasync((timeout, callback) =\u003e {\n\tsetTimeout(() =\u003e callback(null, \"wake up!\"), timeout);\n});\n\nconsole.log(\"Timestamp before: \" + performance.now());\nconsole.log(sleep(1000));\nconsole.log(\"Timestamp after: \" + performance.now());\n```\n\n### `awaitSync(promise)`\n\nSimilar with the keyword `await` but synchronously.\n\n```javascript\nconst { awaitSync } = require(\"@kaciras/deasync\");\n\nconst promise = new Promise(resolve =\u003e setTimeout(resolve, 1000)).then(() =\u003e \"wake up!\")\n\nconsole.log(\"Timestamp before: \" + performance.now());\nconsole.log(awaitSync(promise));\nconsole.log(\"Timestamp after: \" + performance.now());\n```\n\n### `uvRun()`\n\nRun pending callbacks of macro tasks in the event loop.\n\n```javascript\nconst { uvRun } = require(\"@kaciras/deasync\");\n\nlet called = false;\nsetImmediate(() =\u003e called = true);\n\nuvRun();\nconsole.log(`Called is ${called}`); // Called is true\n```\n\n### `runLoopOnce()`\n\nRun micro task callbacks until the queue has been exhausted, then run pending callbacks of macro tasks.\n\n- If a new micro task callback enqueued when executing another, it will also be executed.\n- Callbacks enqueued in macro task callback will not be executed.\n\n### `loopWhile(predicate)`\n\nFor async function with unconventional API, for instance function asyncFunction(p1,function cb(res){}), use loopWhile(predicateFunc) where predicateFunc is a function that returns boolean loop condition.\n\n```javascript\nlet done = false;\nlet data;\nasyncFunction(p1, res =\u003e {\n\tdata = res;\n\tdone = true;\n});\nrequire('deasync').loopWhile(() =\u003e !done);\n// data is now populated\n```\n\n## Recommendation\n\nDeAsync changes code execution sequence and the task scheduling, which typically degrades performance. The primary use case for DeAsync is compatibility with legacy code that does not support asynchronous. If all you are facing is syntactic problem such as callback hell, using a less drastic package implemented in pure js is recommended.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaciras%2Fdeasync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaciras%2Fdeasync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaciras%2Fdeasync/lists"}