Skip to main content

Built and signed on GitHub Actions

A tiny way to make `Promise` more convenient to use without any dependencies.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
8 months ago (1.0.0)

Future

NPM version NPM downloads JSR Version JSR Score Build Status codecov

Future uses less than 10 lines of code to change the usage of Promise.

Allow Promise to call resolve/reject anywhere, just like C# TaskCompletionSource, without being restricted to the executor that creates Promise.

Installation

via pnpm

pnpm add tiny-future

or via yarn

yarn add tiny-future

or just from npm

npm install --save tiny-future

via JSR

jsr add @happy-js/tiny-future

for deno

deno add @happy-js/tiny-future

for bun

bunx jsr add @happy-js/tiny-future

Example

import { Future } from 'tiny-future';

function sleep(ms: number):Promise<number> {
    const future = new Future<number>();

    setTimeout(() => {
        // future.resolve/future.reject at anywhere
        future.resolve(0);
    }, ms);

    return future.promise;
}

await sleep(1000);

If you have used C# TaskCompletionSource, then you should be familiar with the usage of Future.

Compare to the usual way of creating Promise.

function sleep(ms: number): Promise<number> {
    return new Promise((resolve) => {
        setTimeout(() => {
            // resolve/reject must in the executor closure
            resolve(ms);
        }, ms);
    });
}

await sleep(1000);

Docs

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@happy-js/tiny-future

Import symbol

import * as tiny_future from "@happy-js/tiny-future";

---- OR ----

Import directly with a jsr specifier

import * as tiny_future from "jsr:@happy-js/tiny-future";

Add Package

npx jsr add @happy-js/tiny-future

Import symbol

import * as tiny_future from "@happy-js/tiny-future";

Add Package

yarn dlx jsr add @happy-js/tiny-future

Import symbol

import * as tiny_future from "@happy-js/tiny-future";

Add Package

pnpm dlx jsr add @happy-js/tiny-future

Import symbol

import * as tiny_future from "@happy-js/tiny-future";

Add Package

bunx jsr add @happy-js/tiny-future

Import symbol

import * as tiny_future from "@happy-js/tiny-future";