Skip to content
/ ijob Public

Create asynchronous job that can be cancelled during executing

Notifications You must be signed in to change notification settings

linq2js/ijob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ijob

Helper for creating cancellable promise-returning functions.

Create simple job

import job from 'ijob';

async function doSomething() {}
async function cleanup() {}
function processResult() {}

const myJob = job(async ({onCancel, isCancelled}) => {
  onCancel(cleanup);
  const result = await doSomething();
  if (!isCancelled()) {
    processResult(result);
  }
});

myJob.onCancel(() => console.log('job cancelled'));
myJob.onSuccess(() => console.log('job succeeded'));
myJob.onError(() => console.log('job failed'));
myJob.onDone(() => console.log('job done'));
// job.cancel();

Support (Async) Generator

import job from 'ijob';
let count = 0;
const Increase = () => count++;
const Delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

job(function* () {
  for (let i = 0; i < 1000; i++) {
    yield [Delay, 100]; // delay in 100ms
    yield [Increase];
  }
});

Perform the latest job

import job from 'ijob';
const callServerApi = () => {};
const renderProductList = () => {};
const SearchProducts = job.latest(({isCancelled}) => async (term) => {
  const products = await callServerApi(term);
  if (!isCancelled()) {
    renderProductList(products);
  }
});

About

Create asynchronous job that can be cancelled during executing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published