{"id":16811719,"url":"https://github.com/rzvxa/dwait","last_synced_at":"2026-04-09T08:32:48.177Z","repository":{"id":214582061,"uuid":"736536043","full_name":"rzvxa/dwait","owner":"rzvxa","description":"Deferred async operation made easy in JavaScript","archived":false,"fork":false,"pushed_at":"2024-01-03T22:13:05.000Z","size":106,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-16T21:29:15.552Z","etag":null,"topics":["async","async-await","await","fp","functional-programming","javascript","nodejs","promise-chain","rust","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/dwait","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rzvxa.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2023-12-28T07:04:49.000Z","updated_at":"2024-04-07T19:09:59.000Z","dependencies_parsed_at":"2023-12-30T09:26:15.427Z","dependency_job_id":"94203941-ef4a-4fdf-83cb-01ea74bf8196","html_url":"https://github.com/rzvxa/dwait","commit_stats":{"total_commits":54,"total_committers":3,"mean_commits":18.0,"dds":0.2407407407407407,"last_synced_commit":"4b60a653088433e1b9b4a8a78fae54897eb0f472"},"previous_names":["rzvxa/dwait"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/rzvxa/dwait","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzvxa%2Fdwait","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzvxa%2Fdwait/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzvxa%2Fdwait/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzvxa%2Fdwait/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rzvxa","download_url":"https://codeload.github.com/rzvxa/dwait/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzvxa%2Fdwait/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271696018,"owners_count":24805105,"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","status":"online","status_checked_at":"2025-08-22T02:00:08.480Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","async-await","await","fp","functional-programming","javascript","nodejs","promise-chain","rust","typescript"],"created_at":"2024-10-13T10:19:33.862Z","updated_at":"2026-04-09T08:32:43.110Z","avatar_url":"https://github.com/rzvxa.png","language":"TypeScript","readme":"# dwait\n\n![GitHub License](https://img.shields.io/github/license/rzvxa/dwait)\n[![Test](https://github.com/rzvxa/dwait/actions/workflows/test.yml/badge.svg)](https://github.com/rzvxa/dwait/actions/workflows/test.yml)\n![Coveralls branch](https://img.shields.io/coverallsCoverage/github/rzvxa/dwait)\n[![Documentation](https://img.shields.io/badge/visit-docs-brightgreen)](https://rzvxa.github.io/dwait/)\n\nDeffered async operation made easy in Javascript\n\n# What is it?\n`dwait` is an alternative way of handling async tasks. The name stands for `Deferred Await` and it basically lets you defer multiple `await` operations to the end of the function chain, So you can stop writing things like this:\n\n```js\n  const resp = await getUserAsync();\n  const json = await resp.body.toJson();\n  const username = json.username.trim();\n```\n\nAnd instead, write more readable(in my opinion) code like this:\n\n```js\n  const username = await dwait(getUserAsync())\n    .body\n    .toJson()\n    .username\n    .trim()\n    .await; // or .toPromise();\n```\n\nOr you can pass the function itself to the `dwait` function and get a deferred function you can use directly!\n\n```js\n  const getUserDeferred = dwait(getUserAsync);\n  const username = await getUserDeferred()\n    .body\n    .toJson()\n    .username\n    .trim()\n    .await; // or .toPromise();\n```\n\n# Why?\nIf you have ever seen any async code from `Rust` language you can immediately see the source of inspiration for the `dwait` library.\nMost languages have opted-in for writing the `await` keyword before the expression, This way it will read more naturally. For example something like this:\n```js\nawait asyncTask();\n```\nIt will read as `await async task` which is nice, But whenever you get knee-deep into the `async/await` pattern, this style of awaiting can get a little bit annoying in even some trivial cases. Take this code as an example:\n```js\nconst fileNames = (await (await getFilesRepository()).getFileNames()).map((f) =\u003e f.trim().split(\".\"));\n```\nWell to be honest nobody writes code like this, So let's break it into multiple lines to make it readbly.\n```js\nconst fileRepo = await getFilesRepository();\nconst files = await fileRepo.getFileNames();\nconst fileNames = files.map((fullname) =\u003e fullname.trim().split(\".\"));\n```\nNow that's much better, But wouldn't it be nice if we could get rid of extra variables and just chain our operations? Well, that is exactly what the right-hand side awaiting solves, Just imagine if we could write code with await as a function on the `Promise` instead of a keyword, Or even better what if we could just write `.await` at the end of an awaitable expression and it would just get awaited? Unfortunately, that's not possible without language-level support (either via typescript/babel or ECMAScript specification).\nSo what's the next best thing? If we want to get the result from a `Promise` we have to either use `then` callback or `await` the expression. So what if we could wrap the promise inside an object and use it to dispatch the subsequent function calls and/or property accesses inside a new `Promise`? This way we can defer the await keyword to the last expression and we can just `await` that last promise to get the result of whole function chain.\nSo let's rewrite the code above using `dwait`.\n```js\nconst fileNames = await dwait(getFilesRepository())\n  .getFileNames()\n  .map((fullname) =\u003e fullname.trim().split(\".\"))\n  .await;\n```\nMuch cleaner, isn't it?\n# How does it work?\nSo as mentioned earlier `dwait` works in the user space and doesn't need any new language feature to provide an appropriately typed solution for this situation. But how does it work?\nWhenever you want to defer awaiting a chain of async operation you have to wrap the first `Promise\u003cT\u003e` inside a `DeferredPromise\u003cT\u003e`, It can be done by passing the promise into the `dwait` function.\n```js\nconst deferredPromise = dwait(promise);\n```\n`DeferredPromise` extends the javascript underlying `Promise` type, It has anything existing on the type `T` in `DeferredPromise\u003cT\u003e`, But it will return another `DeferredPromise` for the result instead of the actual return type.\n```ts\nconst r1: string = getStringSync().trim();\nconst r2: DeferredPromise\u003cstring\u003e = dwait(getStringAsync()).trim();\n```\nAs you can see, calling `trim` on a `DeferredPromise\u003cstring` will result in a `DeferreedPromise\u003cstring\u003e` instead of the `string` result. This way we can start to chain these `DeferredAsync` operations and at the end, we either call the `toPromise` function or read the value of the `await` field.\n```js\nconst result = await dwait(getStringAsync()).trim().slice(3).split(\" \").toPromise();\n// or\nconst result = await dwait(getStringAsync()).trim().slice(3).split(\" \").await;\n```\nBoth `toPromise` and `await` will return a native `Promise` object which can be used to `await` the final result of the operation chain.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frzvxa%2Fdwait","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frzvxa%2Fdwait","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frzvxa%2Fdwait/lists"}