{"name":"make-asynchronous","version":"0.1.0","keywords":["function","asynchronous","async","thread","dispatch","synchronous","sync","web","worker","thread","cpu","expensive","pool"],"author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"license":"MIT","_id":"make-asynchronous@0.1.0","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"homepage":"https://github.com/sindresorhus/make-asynchronous#readme","bugs":{"url":"https://github.com/sindresorhus/make-asynchronous/issues"},"xo":{"rules":{"n/prefer-global/buffer":"off","n/prefer-global/process":"off"}},"ava":{"workerThreads":false},"dist":{"shasum":"77a1e5579b99cd8ca5fc848f3a5d6bfdc242df17","tarball":"http://registry-npm.ywwl.com/make-asynchronous/download/make-asynchronous-0.1.0.tgz","fileCount":5,"integrity":"sha512-HhwhPTbqf2dwE/swlkQDZ+C8YPQgkWuAMkD9Tet0w5Wo7rF65eCENwy73cP98DAj3opmp6jdDvIRhRYGi6Ukyw==","signatures":[{"sig":"MEYCIQCWKUUL50uzcYDUNBOENW8/kr8698rMOfF6J01+JjKs+QIhAJTdEAS+bsnzHO/WE6RV/A48UqvYiOqi66FwI+FJhD/x","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":6088,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiuNczACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmqc3xAAodISWm7h6U09bcYsZK/sXgxZRD+iZ2VkhL2DMgIsTLjXdyIi\r\ntkcUGIEKmQgWl49VkV/vI643Ba3u8+cx+5Uq5Dv1jz/S7AtFuuX09ph+CiJ0\r\ndnDdTGBczy9gQdhdukPSPn2Gmv8z+u2ITkOZaWA3wPNRHjkVRYQN049O/guK\r\nihH4pYWHkpbfW7IprbGKCN1gO5WTTXi+p3GsDW1y9L9cSM4fEUc/bEQ694RK\r\ngK7XYd+JDUAzuanjw60nGUiUI3nt2ORwJ4caHplP0VskhZqUr0yPcP0Kjqbb\r\nmCvu0fGSWdc2GlZcS5/ep2qz2w/YobTIhBaRVGHx8z+5ixZBVix5pfbiK+Tm\r\nS/hnNXYS58S89hF1GAuD+o8NLYFrLmLsDCxB98zoOwcsgSDW2wIOCElfhcxC\r\nvxCAphLAOX+7zXovPpjUxwc7gO/OCFEYjIbZtWzq/7rFizzwfbNPlcHLrsh3\r\nvVmeZKQHQ2fgjSM9FKpLgJ3eAHzZb96FzozkVnogNnxl0ar8cwcFACLwMVj/\r\nKWTjalh3gh4NkpjvL5wT4sb4b4su7fY5QE5a6y5ZUPuDspKniL7MA0NzOpp7\r\nyROVmnBHIdNyB0HQv64Dt9z1TN6OgpQniYMoGQWX04xiJU3tThgI8tx8wgPV\r\ncegQMnGBvRhVcKWdJ/Pr7zwieFsp3uGBBv4=\r\n=ZXNc\r\n-----END PGP SIGNATURE-----\r\n","size":2681,"noattachment":false,"key":"/make-asynchronous/-/make-asynchronous-0.1.0.tgz"},"type":"module","types":"./index.d.ts","engines":{"node":">=14.16"},"exports":"./index.js","funding":"https://github.com/sponsors/sindresorhus","gitHead":"de08673909684404ebe3f582add17a0e81c9b13f","scripts":{"test":"xo && ava && tsd"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/make-asynchronous.git","type":"git"},"_npmVersion":"8.3.2","description":"Make a synchronous function asynchronous by running it in a worker","directories":{},"_nodeVersion":"14.19.3","dependencies":{"type-fest":"^2.14.0","web-worker":"^1.2.0"},"_hasShrinkwrap":false,"devDependencies":{"xo":"^0.50.0","ava":"^4.3.0","tsd":"^0.21.0","delay":"^5.0.0","in-range":"^3.0.0","time-span":"^5.1.0"},"_npmOperationalInternal":{"tmp":"tmp/make-asynchronous_0.1.0_1656280883037_0.5019612294164111","host":"s3://npm-registry-packages"},"readme":"# make-asynchronous\n\n> Make a synchronous function asynchronous by running it in a [worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)\n\nThis makes it super simple to offload some expensive work without having to deal with the complex Web Workers API.\n\n**Please upvote [this Node.js issue](https://github.com/nodejs/node/issues/43583) 🙏** It would let us simplify the code.\n\n*Works in Node.js and browsers.*\n\n## Install\n\n```sh\nnpm install make-asynchronous\n```\n\n## Usage\n\n```js\nimport makeAsynchronous from 'make-asynchronous';\n\nconst fn = makeAsynchronous(number => {\n\treturn performExpensiveOperation(number);\n});\n\nconsole.log(await fn(2));\n//=> 345342\n```\n\n## API\n\n### makeAsynchronous(function, options?)\n\nReturns a wrapped version of the given function which executes asynchronously in a background thread (meaning it will not block the main thread).\n\nThe 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.\n\n### makeAsynchronousIterable(function, options?)\n\nMake the iterable returned by a function asynchronous by running it in a worker.\n\nThe 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.\n\n```js\nimport {makeAsynchronousIterable} from 'make-asynchronous';\n\nconst fn = makeAsynchronousIterable(function * (number) {\n\tyield * performExpensiveOperation(number);\n});\n\nfor await (const number of fn(2)) {\n\tconsole.log(number);\n}\n```\n\n#### options\n\nType: `object`\n\nThe options are the same for `makeAsynchronous` and `makeAsynchronousIterable`.\n\n##### baseUrl\n\nType: `string | URL`\n\nBase URL used to resolve bare dynamic imports in Node.js workers.\n\nPass `import.meta.url` when the function dynamically imports dependencies from the module that creates the wrapper.\n\n```js\nimport makeAsynchronous from 'make-asynchronous';\n\nconst fn = makeAsynchronous(async () => {\n\tconst {default: package_} = await import('package-owned-by-this-module');\n\treturn package_;\n}, {\n\tbaseUrl: import.meta.url,\n});\n```\n\n#### fn.withSignal(signal)\n\nThe function returned by `makeAsynchronous` and `makeAsynchronousIterable` has an additional method which allows an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to be provided.\n\n```js\nimport makeAsynchronous from 'make-asynchronous';\n\nconst fn = makeAsynchronous(number => {\n\treturn performExpensiveOperation(number);\n});\n\nconst controller = new AbortController();\nconst timeoutId = setTimeout(() => {\n\tcontroller.abort();\n}, 1000); // 1 second timeout\n\nconst result = await fn.withSignal(controller.signal)(2);\nclearTimeout(timeoutId);\n\nconsole.log(result);\n//=> 345342\n```\n\n## Related\n\n- [make-synchronous](https://github.com/sindresorhus/make-synchronous) - Make an asynchronous function synchronous\n- [sleep-synchronously](https://github.com/sindresorhus/sleep-synchronously) - Block the main thread for a given amount of time\n","publish_time":1656280883215,"_cnpm_publish_time":1656280883215,"dist-tags":{"latest":"2.1.0"}}