# @ashnazg/quantum-promise

> a promise that runs its creator only when you try to observe the results

Latest version **2.0.0** (published 2023-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ashnazg/quantum-promise
pnpm add @ashnazg/quantum-promise
yarn add @ashnazg/quantum-promise
bun add @ashnazg/quantum-promise
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-11-29 |
| First published | 2021-10-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Xander |
| Maintainers | xander |

## Links

- npm: https://www.npmjs.com/package/@ashnazg/quantum-promise
- npm.io page: https://npm.io/package/@ashnazg/quantum-promise

## Recent versions

- 2.0.0 (latest) — 2023-11-29
- 1.0.0 — 2023-05-07
- 0.0.2 — 2021-10-26
- 0.0.1 — 2021-10-12

## README

## Quantum Promise

<!-- ~/projects/ashnazg-npm/quantum-promise/qp.js -->

It's like a promise, but with more lazy procrastination: it only starts once you await or .then() it.
```js
const qp = require('quantum-promise'); // https://ashnazg.com/docs/lib/quantum-promise

async function dowork() {
	console.log("running");
	await q.delay(1000);
	console.log("done");
}

const promise = qp(dowork);
console.log("about to run");
await promise;
```

## Is used as a mix-in
An object can become an awaitable deferred action by using this as a mix-in, and can replace the `run()` behavior after creation but before awaiting:

```js
class Foo {
	constructor() {
		Object.assign(this, qp(this.doDefault));
	}

	doDefault() {
		console.log("default action that's triggered by await");
	}

	configEdgeCase() {
		this[qp.run] = () => {
			console.log("running edge case");
		};
	}
}

const foo = new Foo();
foo.configEdgeCase();
await foo;
```

All this weirdness allows squirrelnado to express ETL jobs with the following `await => commence` syntax, because I dislike verbose boilerplate:
```
const {read} = require('@ashnazg/squirrelnado');
const rows = await read('db://endpoint/tablefoo'); // the lack of any explicit termination means "resolve to array of rows"

// or instead of calling await on the opening step, chain them and await the last:
await read('db://endpoint/tablefoo').map(row => {
	row.calc = 42;
	return row;
}).write('db://endpoint/report_table');
```

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