# sync-await

> Synchronously run async-await if possible.

Latest version **0.0.3** (published 2019-09-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install sync-await
pnpm add sync-await
yarn add sync-await
bun add sync-await
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2019-09-04 |
| First published | 2019-09-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ccorcos |

## Links

- npm: https://www.npmjs.com/package/sync-await
- npm.io page: https://npm.io/package/sync-await

## Recent versions

- 0.0.3 (latest) — 2019-09-04
- 0.0.2 — 2019-09-04
- 0.0.1 — 2019-09-04

## README

# Sync Await

I want to be able to code that can optionally use an asynchronous database or a synchronous in-memory database. The best option for doing this would be coroutines / generators. However, Typescript doesn't have good type support for generators. Even TypeScript 3.6 doesn't let your associate the left and right side of a `yield`. Another related concept here is algebraic datatypes.

Long story short, I've created a hack where you can use async/await in TypeScript but have `await` evaluate synchronously if the Promise resolves synchronously.

## Usage

```ts
import * as SyncAwait from "sync-await"

// __awaiter and __generator need to be defined at the top of the file. These will
// get picked up by the async/await polyfill so long as your tsconfig.json outputs es5.
const {__awaiter, __generator, runSync} = SyncAwait


// Suppose there is a sync way to do things and an async way to do things.
const sync = {
	a: () => true
}
const async = {
	a: () => new Promise(resolve => setTimeout(() => resolve(true), 1))
}

// Some business logic that can use either sync or async api.
async function f(api: typeof sync | typeof async) {
	const result = await api.a()
	return result
}

const asyncResult = f(async) // returns as Promise<true>
const syncResult = runSync(f(sync)) // returns true
```

---
_Source: https://npm.io/package/sync-await · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
