Skip to content

vadim-ch/webworker-promisify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

webworker-promisify

webworker-promisify is wrapper over a Webworker that allows to execute a function in a separate thread and returned Promise.

How to use

npm install webworker-promisify

import { webworkerPromisify } from 'webworker-promisify';

Examples on TypeScript

Promise

let sum = (a, b) => {
  return a + b;
};
sum = webworkerPromisify<number>(sum);

sum(1, 2).then((data) => {
  console.log('data', data);
}).catch((err) => {
  console.error('err', err);
});
const prepareData = data => {
  return data.map(item => (item.prepared = true, item));
};

sendRequest(id) // returned Promise with large array
  .then(data => {
    return webworkerPromisify(prepareData)(data); // changed data in Worker
  })
  .then(preparedData => {
    console.log('preparedData', preparedData); // returned preparedData
  });

Async await

let sum = (a, b) => {
  return a + b;
};
sum = webworkerPromisify<number>(sum);

try {
  const data = await sum(1, 2);
  console.log('data', data);
} catch (err) {
  console.error('err', err);
}
const prepareData = data => {
  return data.map(item => (item.prepared = true, item));
};

const data = await sendRequest(id); // returned Promise with large array
const preparedData = await webworkerPromisify(prepareData)(data);

About

Npm module for Webworker

Resources

Stars

Watchers

Forks

Packages

No packages published