import { Task, retry } from "@daiso-tech/core/task";
const promise = new Task(async () => {
console.log("I am lazy");
},
// You can also pass in one AsyncMiddleware or multiple (as an Array).
retry()
);
// "I am lazy" will only logged when awaited or then method i called.
await promise;
You can pass sync or async Invokable.
The detach method executes the Task without awaiting it.
import { Task } from "@daiso-tech/core/task";
import { TimeSpan } from "@daiso-tech/core/time-span" from "@daiso-tech/core/time-span";
const promise =
new Task(async () => {
await Task.delay(TimeSpan.fromSeconds(1));
// Will be loged after one second
console.log("Done !");
});
promise.detach();
// Will be logged immediately
console.log("Hello");
await Task.delay(TimeSpan.fromSeconds(2));
The pipe method returns a new Task instance with the additional middlewares applied.
The pipeWhen method conditionally applies additional middlewares, returning a new Task instance only if the specified condition is met.
Attaches callbacks for the resolution and/or rejection of the Promise.
A Promise for the completion of which ever callback is executed.
StaticallThe all method works similarly to Promise.all | Promise.all with the key distinction that it operates lazily.
The all method works similarly to Promise.all | Promise.all with the key distinction that it operates lazily.
StaticallThe allSettled method works similarly to Promise.allSettled | Promise.allSettled with the key distinction that it operates lazily.
The allSettled method works similarly to Promise.allSettled | Promise.allSettled with the key distinction that it operates lazily.
StaticanyThe any method works similarly to Promise.any | Promise.any with the key distinction that it operates lazily.
The any method works similarly to Promise.any | Promise.any with the key distinction that it operates lazily.
StaticdelayStaticfromThe fromCallback is convience method used for wrapping Node js callback functions with a Task.
import { Task } from "@daiso-tech/core/task";
import { readFile } from "node:fs";
const task = Task.fromCallback<Buffer | string>((resolve, reject) => {
readFile("FILE_PATH", (err, data) => {
if (err !== null) {
reject(err);
return;
}
resolve(data);
});
});
const file = await task;
console.log(file);
StaticraceThe race method works similarly to Promise.race | Promise.race with the key distinction that it operates lazily.
The race method works similarly to Promise.race | Promise.race with the key distinction that it operates lazily.
StaticrejectStaticresolveThe resolve method works similarly to Promise.resolve | Promise.resolve with the key distinction that it operates lazily.
The resolve method works similarly to Promise.resolve | Promise.resolve with the key distinction that it operates lazily.
StaticwrapThe wrapFn is convience method used for wrapping async Invokable with a Task.
import { Task, retry } from "@daiso-tech/core/task";
import { TimeSpan } from "@daiso-tech/core/time-span" from "@daiso-tech/core/time-span";
import { readFile as readFileNodeJs } from "node:fs/promises";
const readFile = Task.wrapFn(readFileNodeJs);
const file = await readFile("none_existing_file.txt");
The
Taskclass is used for creating lazy PromiseLike |PromiseLikeobject that will only execute when awaited or whenthenmethod is called. Note the class is immutable.IMPORT_PATH:
"@daiso-tech/core/task"