1.0.0 • Published 1 year ago
tiny-future v1.0.0
Future
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-futureor via yarn
yarn add tiny-futureor just from npm
npm install --save tiny-futurevia JSR
jsr add @happy-js/tiny-futurefor deno
deno add @happy-js/tiny-futurefor bun
bunx jsr add @happy-js/tiny-futureExample
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
1.0.0
1 year ago