GithubHelp home page GithubHelp logo

sindresorhus / make-asynchronous Goto Github PK

View Code? Open in Web Editor NEW
243.0 5.0 4.0 19 KB

Make a synchronous function asynchronous by running it in a worker

License: MIT License

JavaScript 92.77% TypeScript 7.23%

make-asynchronous's Introduction

make-asynchronous

Make a synchronous function asynchronous by running it in a worker

This makes it super simple to offload some expensive work without having to deal with the complex Web Workers API.

Please upvote this Node.js issue ๐Ÿ™ It would let us reduce the amount of dependencies and simplify the code.

Works in Node.js and browsers.

Install

npm install make-asynchronous

Usage

import makeAsynchronous from 'make-asynchronous';

const fn = makeAsynchronous(number => {
	return performExpensiveOperation(number);
});

console.log(await fn(2));
//=> 345342

API

makeAsynchronous(function)

Returns a wrapped version of the given function which executes asynchronously in a background thread (meaning it will not block the main thread).

The given function is serialized, so you cannot use any variables or imports from outside the function scope. You can instead pass in arguments to the function.

makeAsynchronousIterable(function)

Make the iterable returned by a function asynchronous by running it in a worker.

import {makeAsynchronousIterable} from 'make-asynchronous';

const fn = makeAsynchronousIterable(function * (number) {
	yield * performExpensiveOperation(number);
});

for await (const number of fn(2)) {
	console.log(number);
}

fn.withSignal(signal)

The function returned by makeAsynchronous and makeAsynchronousIterable has an additional method which allows an AbortSignal to be provided.

import makeAsynchronous from 'make-asynchronous';

const fn = makeAsynchronous(number => {
	return performExpensiveOperation(number);
});

const controller = new AbortController();
const timeoutId = setTimeout(() => {
	controller.abort();
}, 1000); // 1 second timeout

const result = await fn.withSignal(controller.signal)(2);
clearTimeout(timeoutId);

console.log(result);
//=> 345342

Related

make-asynchronous's People

Contributors

richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

make-asynchronous's Issues

AbortController support

AbortSignals should be accepted though they are only supported since Node.js 16. What to do for Node.js 14? Perhaps we run a loose check for AbortSignal so that third-party implementations could be used in older versions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.