Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ensurePromise): add ensurePromise function #41

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ worker(2);
worker(3);
```

### ensurePromise

`ensurePromise` is a utility function that ensures a value is a promise.

```ts
import { ensurePromise } from "@core/asyncutil/ensure-promise";

const p1 = ensurePromise(Promise.resolve("Resolved promise"));
console.log(await p1); // Resolved promise

const p2 = ensurePromise("Not a promise");
console.log(await p2); // Not a promise
```

### Lock/RwLock

`Lock` is a mutual exclusion lock that provides safe concurrent access to a
Expand Down
2 changes: 2 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
".": "./mod.ts",
"./async-value": "./async_value.ts",
"./barrier": "./barrier.ts",
"./ensure-promise": "./ensure_promise.ts",
"./lock": "./lock.ts",
"./mutex": "./mutex.ts",
"./notify": "./notify.ts",
Expand Down Expand Up @@ -34,6 +35,7 @@
"@core/asyncutil": "./mod.ts",
"@core/asyncutil/async-value": "./async_value.ts",
"@core/asyncutil/barrier": "./barrier.ts",
"@core/asyncutil/ensure-promise": "./ensure_promise.ts",
"@core/asyncutil/lock": "./lock.ts",
"@core/asyncutil/mutex": "./mutex.ts",
"@core/asyncutil/notify": "./notify.ts",
Expand Down
20 changes: 20 additions & 0 deletions ensure_promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Ensure that a value is a promise.
*
* It returns the value if it is already a promise, otherwise it returns a
* promise that resolves to the value.
*
* @param value - The value to ensure as a promise.
* @returns A promise that resolves to the value.
*
* ```ts
* import { assertEquals } from "@std/assert";
* import { ensurePromise } from "@core/asyncutil/ensure-promise";
*
* assertEquals(await ensurePromise(42), 42);
* assertEquals(await ensurePromise(Promise.resolve(42)), 42);
* ```
*/
export function ensurePromise<T>(value: T): Promise<T> {
return value instanceof Promise ? value : Promise.resolve(value);
}
12 changes: 12 additions & 0 deletions ensure_promise_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { ensurePromise } from "./ensure_promise.ts";

test("ensurePromise() returns the value if it is already a promise", async () => {
const p = Promise.resolve(42);
assertEquals(await ensurePromise(p), 42);
});

test("ensurePromise() returns a promise that resolves to the value", async () => {
assertEquals(await ensurePromise(42), 42);
});