Job
Job is a simple library for cancellable asynchronous tasks. Job was inspired by Kotlin coroutines and tries to bring
some of the same functionality to TypeScript/JavaScript.
Documentation
Installation
job
can be installed using NPM or Yarn. The scripts provided by the NPM package are UMD scripts and will also work as
script tags. If using script tags, you must load the UMD scripts for @ethossoftworks/outcome
before the scripts for
@ethossoftworks/job
.
yarn add @ethossoftworks/job
Usage
Simple Job
import { Job } from "@ethossoftworks/job"
const result = await new Job(async (job) => {
await job.delay(1000)
return Outcome.ok("Success")
}).run()
Cancel a Job
Every job is cancellable. Cancelling will stop the job at the next "pause" point.
Pause points are created with job.pause()
or the inbuilt job.delay()
import { Job } from "@ethossoftworks/job"
const job = new Job(async (job) => {
await job.delay(1000)
return Outcome.ok("Success")
})
setTimeout(() => job.cancel(), 500) // This stops the job after 500 milliseconds
const result = await job.run()
if (Job.isCancelled(result)) {
// This block will run
}
Children Jobs
Jobs may have children jobs launched from them. When the parent completes or is cancelled, children jobs will be cancelled if they have not already completed.
import { Job } from "@ethossoftworks/job"
// Launch multiple children in parallel
new Job(async (job) => {
const child1 = job.launchAndRun(async (job) => {
await job.delay(2000)
return Outcome.ok(true)
})
const child2 = job.launchAndRun(async (job) => {
await job.delay(1000)
return Outcome.ok(true)
})
const results = await Promise.all([child1, child2])
return Outcome.ok("Success")
})
// Launch multiple children sequentially
new Job(async (job) => {
const child1 = await job.launchAndRun(async (job) => {
await job.delay(2000)
return Outcome.ok(true)
})
const child2 = await job.launchAndRun(async (job) => {
await job.delay(1000)
return Outcome.ok(true)
})
return Outcome.ok("Success")
})
Build Info
Build
yarn build
or yarn build-dev
Develop
- Open two terminals
- Run
yarn start-ts
in the first - Run
yarn start-bundler
in the second
Test
yarn build-test
or yarn test