npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

awork

v0.0.3

Published

Deal with Web Workers in usual async way

Readme

Awork

Build Status

The simplest way to execute code in a separate Web Worker process

Awork is a small library meant to simplify Web Workers usage in some cases. Just wrap or decorate your function with awork and continue using it in a usual async way:


const parsed = await awork(parseJSON)(heavyJSON)

const users = await awork(yourFetchUsersFunc)()

However, be aware of some limitations:

Limitations

  • Only pure functions without side effects and external dependencies can be wrapped at the moment. All necessary data should be passed to this function via arguments.
  • The function should return computed value, which then will be used to fulfill the promise.
  • Polyfilled features will not work correctly inside a web worker process due to external dependencies. For example, Promise polyfills will refer to variables outside the function scope, like _es6Promise (es6-promise) or _promise2 (babel-polyfill).
  • If you are using awork as a decorator for a class method, remember that this inside the method doesn't refer to the class instance.

Installation

Via npm:

$ npm install awork --save

Basic examples:

As a decorator:

import awork from 'awork'

class ClassWithFibonacciNumbers {
    @awork()
    getFibonacciNumber(num) {
        let a = 1, b = 0, tmp
        while (num > 0){
            tmp = a
            a = a + b
            b = tmp
            num--
        }
        return a
    }
}

let instance = new ClassWithFibonacciNumbers()
instance.getFibonacciNumber(100)
    .then((res) => console.log('100th number:', res))
    .catch((err) => console.error(err))

As a wrapper:

import awork from 'awork'

function inefficientFibonacci(num) {
    let a = 1, b = 0, tmp = null
    while (num > 0){
        tmp = a
        a = a + b
        b = tmp
        num--
    }
    return b
}

let fib = awork(inefficientFibonacci)
Promise.all(
    [...Array(100)].map((v, i) => fib(i))
)
    .then(num => print(`First 100 Fibonacci numbers:`, num.join(', ')))
    .catch(error => console.error(error))

Error handling

All errors from web worker will propagate back to the main process and lead to Promise rejection:

const awork = require('awork')

function parseJSON(str) {
    return JSON.parse(str)
}

awork(parseJSON)('{not even close to valid JSON}')
    .then(() => console.log('Surprise'))
    .catch((err) => console.log('Well, it was expected:', err))

Options

Creating a new web worker every call is quite expensive, that's why awork keeps and reuses the same instance by default. But if you are going to call your function only once or twice - you can notify awork that there is no need to keep this web worker instance alive. To do this, pass {keepAlive: false} to awork:

//Config decorator
@awork({keepAlive: false})
fn (args) {
    // ...
}

//Config wrapper
let afn = awork(fn, {keepAlive: false})

Examples

To run examples locally, use:

  1. Run yarn run build:examples && yarn run start:examples
  2. Open http://127.0.0.1:8080 to explore examples folder.

License

MIT License