Skip to content
/ future Public

TypeScript: Minimalistic lazy Promise aka Future

License

Notifications You must be signed in to change notification settings

c-antin/future

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Future

TypeScript: Minimalistic lazy Promise aka Future

Future extends Promise and is only executed when awaited. Can be awaited multiple times.

Example

import { Future } from "https://deno.land/x/[email protected]/mod.ts";

let n = 0;
const future = new Future<number>((resolve) => {
    n++;
    console.log("executor", n);
    resolve(n);
});
console.log(future); // prints `Future [Promise] { <pending> }`
console.assert(n === 0);

{
    const m = await future; // prints `executor 1`
    console.assert(m === 1);
}
console.log(future); // prints `Future [Promise] { <pending> }`
console.assert(n === 1);

{
    const m = await future; // prints `executor 2`
    console.assert(m === 2);
}
console.log(future); // prints `Future [Promise] { <pending> }`
console.assert(n === 2);

About

TypeScript: Minimalistic lazy Promise aka Future

Resources

License

Stars

Watchers

Forks

Packages

No packages published