Skip to content
On this page

Quantum Promise

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');

JavaScript/Bash source released under the MIT License.