{"_id":"@poppinss/defer","_rev":"4-9615de26e6ab85a47af00499c2f2d6f5","name":"@poppinss/defer","dist-tags":{"latest":"1.1.3"},"versions":{"1.1.0":{"name":"@poppinss/defer","version":"1.1.0","keywords":["defer","queue","memory-queue"],"author":{"name":"Harminder Virk","email":"virk@adonisjs.com"},"license":"MIT","_id":"@poppinss/defer@1.1.0","maintainers":[{"name":"julien-r44","email":"julien@ripouteau.com"},{"name":"romainlanz","email":"romain.lanz+npm@pm.me"},{"name":"virk","email":"virk@adonisjs.com"}],"homepage":"https://github.com/poppinss/defer#readme","bugs":{"url":"https://github.com/poppinss/defer/issues"},"c8":{"exclude":["tests/**"],"reporter":["text","html"]},"dist":{"shasum":"4f253a73383d226a8dd95a7917bcc66c34d9ad5f","tarball":"https://registry.npmjs.org/@poppinss/defer/-/defer-1.1.0.tgz","fileCount":10,"integrity":"sha512-+hkwxhKdxbmfTNo9MJrY2Rz7GrMokgx71zSSnEv6/pftaCQ8+bF5vQkE7KCBIKUn+kCth1cI1d8LWDMvPUIRMw==","signatures":[{"sig":"MEUCIQCrKNtyCi3Ha2JaiNJjhuAaPe96VwksHv65fZ69p6cLGAIgDP26mPwEgaCk2x6czqOAspg4HDRtOES4U+H1GGopsVM=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@poppinss%2fdefer@1.1.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":22913},"main":"build/index.js","tsup":{"dts":false,"clean":true,"entry":["index.ts","src/types.ts"],"format":"esm","outDir":"./build","target":"esnext","sourcemap":true},"type":"module","types":"./build/index.d.ts","engines":{"node":">=20.6.0"},"exports":{".":"./build/index.js"},"gitHead":"88798d9d2ab5f1cdf0e8595f7bd561bf8a6d273a","scripts":{"lint":"eslint .","test":"c8 npm run quick:test","build":"npm run compile","format":"prettier --write .","compile":"tsup-node && tsc --emitDeclarationOnly --declaration","pretest":"npm run lint","release":"release-it","version":"npm run build","typecheck":"tsc --noEmit","precompile":"npm run lint","quick:test":"node --import=ts-node-maintained/register/esm --enable-source-maps bin/test.ts","prepublishOnly":"npm run build"},"_npmUser":{"name":"virk","email":"virk@adonisjs.com"},"prettier":"@adonisjs/prettier-config","release-it":{"git":{"push":true,"tagName":"v${version}","commitMessage":"chore(release): ${version}","tagAnnotation":"v${version}","requireUpstream":true,"requireCleanWorkingDir":true},"npm":{"publish":true,"skipChecks":true},"github":{"release":true},"plugins":{"@release-it/conventional-changelog":{"preset":{"name":"angular"}}}},"repository":{"url":"git+https://github.com/poppinss/defer.git","type":"git"},"_npmVersion":"10.8.2","description":"A managed deferred queue to run async operations in the background","directories":{},"_nodeVersion":"20.17.0","dependencies":{"fastq":"^1.17.1"},"publishConfig":{"access":"public","provenance":true},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","tsup":"^8.2.4","eslint":"^9.10.0","prettier":"^3.3.3","@swc/core":"^1.7.26","release-it":"^17.6.0","typescript":"^5.6.2","@types/node":"^22.5.5","@japa/assert":"^3.0.0","@japa/expect":"^3.0.2","@japa/runner":"^3.1.4","@japa/expect-type":"^2.0.2","@japa/file-system":"^2.3.0","@adonisjs/tsconfig":"^1.4.0","ts-node-maintained":"^10.9.4","@adonisjs/eslint-config":"^2.0.0-beta.6","@adonisjs/prettier-config":"^1.4.0","@release-it/conventional-changelog":"^8.0.2"},"_npmOperationalInternal":{"tmp":"tmp/defer_1.1.0_1726335542372_0.2535795567491894","host":"s3://npm-registry-packages"}},"1.1.1":{"name":"@poppinss/defer","version":"1.1.1","keywords":["defer","queue","memory-queue"],"author":{"name":"Harminder Virk","email":"virk@adonisjs.com"},"license":"MIT","_id":"@poppinss/defer@1.1.1","maintainers":[{"name":"virk","email":"virk@adonisjs.com"},{"name":"romainlanz","email":"romain.lanz+npm@pm.me"},{"name":"julien-r44","email":"julien@ripouteau.com"}],"homepage":"https://github.com/poppinss/defer#readme","bugs":{"url":"https://github.com/poppinss/defer/issues"},"c8":{"exclude":["tests/**"],"reporter":["text","html"]},"dist":{"shasum":"9c28ee617097c863ff78b62e0afbf6c4902e1622","tarball":"https://registry.npmjs.org/@poppinss/defer/-/defer-1.1.1.tgz","fileCount":8,"integrity":"sha512-ijTvg5/eduj7gPfHH2k3qId9GvCTQIDOZw+BjlEYE2T4zycqMUG0WX4GwSQHdBSzxbLEn4lavtGmUsjRDBBBKQ==","signatures":[{"sig":"MEQCIGU/Dnfj7uY2geAo2+6qP+hFp0i+VP2ayCEJNTkZ3qxJAiBASoO6q8vp4ZshW1ZJ4BJdQ/Zh5hUQrN4XpX99GXecYw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@poppinss%2fdefer@1.1.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":15568},"main":"build/index.js","tsup":{"dts":false,"clean":true,"entry":["index.ts","src/types.ts"],"format":"esm","outDir":"./build","target":"esnext","sourcemap":false},"type":"module","types":"./build/index.d.ts","engines":{"node":">=20.6.0"},"exports":{".":"./build/index.js"},"gitHead":"5d9e71d39558a8974de10b309685c8100d0e6d22","scripts":{"lint":"eslint .","test":"c8 npm run quick:test","build":"npm run compile","format":"prettier --write .","compile":"tsup-node && tsc --emitDeclarationOnly --declaration","pretest":"npm run lint","release":"release-it","version":"npm run build","typecheck":"tsc --noEmit","precompile":"npm run lint","quick:test":"node --import=ts-node-maintained/register/esm --enable-source-maps bin/test.ts","prepublishOnly":"npm run build"},"_npmUser":{"name":"virk","email":"virk@adonisjs.com"},"prettier":"@adonisjs/prettier-config","release-it":{"git":{"push":true,"tagName":"v${version}","commitMessage":"chore(release): ${version}","tagAnnotation":"v${version}","requireUpstream":true,"requireCleanWorkingDir":true},"npm":{"publish":true,"skipChecks":true},"github":{"release":true},"plugins":{"@release-it/conventional-changelog":{"preset":{"name":"angular"}}}},"repository":{"url":"git+https://github.com/poppinss/defer.git","type":"git"},"_npmVersion":"10.8.2","description":"A managed deferred queue to run async operations in the background","directories":{},"_nodeVersion":"20.18.1","dependencies":{"fastq":"^1.18.0"},"publishConfig":{"access":"public","provenance":true},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.3","tsup":"^8.3.5","eslint":"^9.17.0","prettier":"^3.4.2","@swc/core":"^1.10.2","release-it":"^17.11.0","typescript":"^5.7.2","@types/node":"^22.10.2","@japa/assert":"^4.0.0","@japa/expect":"^3.0.2","@japa/runner":"^3.1.4","@japa/expect-type":"^2.0.2","@japa/file-system":"^2.3.1","@adonisjs/tsconfig":"^1.4.0","ts-node-maintained":"^10.9.4","@adonisjs/eslint-config":"^2.0.0-beta.6","@adonisjs/prettier-config":"^1.4.0","@release-it/conventional-changelog":"^9.0.4"},"_npmOperationalInternal":{"tmp":"tmp/defer_1.1.1_1735294160719_0.8910569508535668","host":"s3://npm-registry-packages-npm-production"}},"1.1.2":{"name":"@poppinss/defer","version":"1.1.2","keywords":["defer","queue","memory-queue"],"author":{"name":"Harminder Virk","email":"virk@adonisjs.com"},"license":"MIT","_id":"@poppinss/defer@1.1.2","maintainers":[{"name":"virk","email":"virk@adonisjs.com"},{"name":"romainlanz","email":"romain.lanz+npm@pm.me"},{"name":"julien-r44","email":"julien@ripouteau.com"}],"homepage":"https://github.com/poppinss/defer#readme","bugs":{"url":"https://github.com/poppinss/defer/issues"},"c8":{"exclude":["tests/**"],"reporter":["text","html"]},"dist":{"shasum":"087323679469a195d99b5162daa928fda88e2e9c","tarball":"https://registry.npmjs.org/@poppinss/defer/-/defer-1.1.2.tgz","fileCount":8,"integrity":"sha512-cJbMauN3dH09jpjXv179QXjfv1/fickfDlf27jTOPWlDlG1E/YgfRVPgzDgyOWYqmRbTpvZ28FWHXZxlDjavuw==","signatures":[{"sig":"MEUCIQCCobTLISUjD2zQhmmfwc3yiqc4V8f3pwAAzQptUDqRZgIgWqmLLaP6W7PVcMocwBPxJYgcRii9IdQoXXSHp2zc7xs=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@poppinss%2fdefer@1.1.2","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":15526},"main":"build/index.js","tsup":{"dts":false,"clean":true,"entry":["index.ts","src/types.ts"],"format":"esm","outDir":"./build","target":"esnext","sourcemap":false},"type":"module","types":"./build/index.d.ts","exports":{".":"./build/index.js"},"gitHead":"26cac95065cc4d0ed9472fe864d509835becb21f","scripts":{"lint":"eslint .","test":"c8 npm run quick:test","build":"npm run compile","format":"prettier --write .","compile":"tsup-node && tsc --emitDeclarationOnly --declaration","pretest":"npm run lint","release":"release-it","version":"npm run build","typecheck":"tsc --noEmit","precompile":"npm run lint","quick:test":"node --import=@poppinss/ts-exec --enable-source-maps bin/test.ts","prepublishOnly":"npm run build"},"_npmUser":{"name":"virk","actor":{"name":"virk","type":"user","email":"virk@adonisjs.com"},"email":"virk@adonisjs.com"},"prettier":"@adonisjs/prettier-config","release-it":{"git":{"push":true,"tagName":"v${version}","commitMessage":"chore(release): ${version}","tagAnnotation":"v${version}","requireUpstream":true,"requireCleanWorkingDir":true},"npm":{"publish":true,"skipChecks":true},"github":{"release":true},"plugins":{"@release-it/conventional-changelog":{"preset":{"name":"angular"}}}},"repository":{"url":"git+https://github.com/poppinss/defer.git","type":"git"},"_npmVersion":"11.4.2","description":"A managed deferred queue to run async operations in the background","directories":{},"_nodeVersion":"24.3.0","dependencies":{"fastq":"^1.19.1"},"publishConfig":{"access":"public","provenance":true},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.3","tsup":"^8.5.0","eslint":"^9.30.1","prettier":"^3.6.2","@swc/core":"^1.12.9","release-it":"^19.0.3","typescript":"^5.8.3","@types/node":"^24.0.10","@japa/assert":"^4.0.1","@japa/expect":"^3.0.4","@japa/runner":"^4.2.0","@japa/expect-type":"^2.0.3","@japa/file-system":"^2.3.2","@poppinss/ts-exec":"^1.4.0","@adonisjs/tsconfig":"^2.0.0-next.0","@adonisjs/eslint-config":"^3.0.0-next.0","@adonisjs/prettier-config":"^1.4.5","@release-it/conventional-changelog":"^10.0.1"},"_npmOperationalInternal":{"tmp":"tmp/defer_1.1.2_1751621696397_0.3401072384400623","host":"s3://npm-registry-packages-npm-production"}},"1.1.3":{"name":"@poppinss/defer","description":"A managed deferred queue to run async operations in the background","version":"1.1.3","type":"module","main":"build/index.js","exports":{".":"./build/index.js"},"scripts":{"pretest":"npm run lint","test":"c8 npm run quick:test","lint":"eslint .","format":"prettier --write .","typecheck":"tsc --noEmit","precompile":"npm run lint","compile":"tsdown && tsc --emitDeclarationOnly --declaration","build":"npm run compile","version":"npm run build","prepublishOnly":"npm run build","release":"release-it","quick:test":"node --import=@poppinss/ts-exec --enable-source-maps bin/test.ts"},"devDependencies":{"@adonisjs/eslint-config":"^3.0.0-next.5","@adonisjs/prettier-config":"^1.4.5","@adonisjs/tsconfig":"^2.0.0-next.3","@japa/assert":"^4.1.1","@japa/expect":"^3.0.6","@japa/expect-type":"^2.0.3","@japa/file-system":"^2.3.2","@japa/runner":"^4.4.0","@poppinss/ts-exec":"^1.4.1","@release-it/conventional-changelog":"^10.0.3","@types/node":"^25.0.1","c8":"^10.1.3","eslint":"^9.39.1","prettier":"^3.7.4","release-it":"^19.1.0","tsdown":"^0.17.3","typescript":"^5.9.3"},"dependencies":{"fastq":"^1.19.1"},"homepage":"https://github.com/poppinss/defer#readme","repository":{"type":"git","url":"git+https://github.com/poppinss/defer.git"},"bugs":{"url":"https://github.com/poppinss/defer/issues"},"keywords":["defer","queue","memory-queue"],"author":{"name":"Harminder Virk","email":"virk@adonisjs.com"},"license":"MIT","publishConfig":{"access":"public","provenance":true},"tsdown":{"entry":["index.ts","src/types.ts"],"outDir":"./build","clean":true,"format":"esm","minify":"dce-only","fixedExtension":false,"dts":false,"treeshake":false,"sourcemaps":false,"target":"esnext"},"release-it":{"git":{"requireCleanWorkingDir":true,"requireUpstream":true,"commitMessage":"chore(release): ${version}","tagAnnotation":"v${version}","push":true,"tagName":"v${version}"},"github":{"release":true},"npm":{"publish":true,"skipChecks":true},"plugins":{"@release-it/conventional-changelog":{"preset":{"name":"angular"}}}},"c8":{"reporter":["text","html"],"exclude":["tests/**"]},"prettier":"@adonisjs/prettier-config","gitHead":"a6c611c9a8d3472b66cee5ef2758553d73e8cff9","types":"./build/index.d.ts","_id":"@poppinss/defer@1.1.3","_nodeVersion":"24.11.1","_npmVersion":"11.6.2","dist":{"integrity":"sha512-icj9T5xDnkwuu+434ngyHfxw6wWtC2wvcQwZai8WSsartbhVeA7Jef+V1PUCP3hU0rA48qj7EI+XTDC8Jag8tA==","shasum":"2ec3b95497af3108011708fc5ebe3ece699dc70f","tarball":"https://registry.npmjs.org/@poppinss/defer/-/defer-1.1.3.tgz","fileCount":8,"unpackedSize":13722,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@poppinss%2fdefer@1.1.3","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIAzjr4QJbMpi8/9WnArOBZKLklEMB3bRud7WVcrTuKYhAiAXCy8T4FQq0ldr8i4QzlMOFjBxXQGhCTbqhI1lYCHgAA=="}]},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:69635c97-cba0-4a63-9917-148399f8b41f"}},"directories":{},"maintainers":[{"name":"virk","email":"virk@adonisjs.com"},{"name":"romainlanz","email":"romain.lanz+npm@pm.me"},{"name":"julien-r44","email":"julien@ripouteau.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/defer_1.1.3_1765533003018_0.09132627388860293"},"_hasShrinkwrap":false}},"time":{"created":"2024-09-14T17:39:02.258Z","modified":"2025-12-12T09:50:03.835Z","1.1.0":"2024-09-14T17:39:02.514Z","1.1.1":"2024-12-27T10:09:20.894Z","1.1.2":"2025-07-04T09:34:56.573Z","1.1.3":"2025-12-12T09:50:03.239Z"},"bugs":{"url":"https://github.com/poppinss/defer/issues"},"author":{"name":"Harminder Virk","email":"virk@adonisjs.com"},"license":"MIT","homepage":"https://github.com/poppinss/defer#readme","keywords":["defer","queue","memory-queue"],"repository":{"type":"git","url":"git+https://github.com/poppinss/defer.git"},"description":"A managed deferred queue to run async operations in the background","maintainers":[{"name":"virk","email":"virk@adonisjs.com"},{"name":"romainlanz","email":"romain.lanz+npm@pm.me"},{"name":"julien-r44","email":"julien@ripouteau.com"}],"readme":"# @poppinss/defer\n\n<br />\n\n[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]\n\n## Introduction\n\nThe `@poppinss/defer` package allows you to run async operations in the background using an in-memory queue. Think of it as `setImmediate` but with support for **monitoring**, **error handling**, and the ability to **gracefully shutdown process** by flushing the queue.\n\n## Installation and usage\n\nUse the following command to install the package from the npm packages registry.\n\n```sh\nnpm i @poppinss/defer\n```\n\nOnce installed, you can create an instance of the `DeferQueue` class and start pushing tasks to it using the `push` method.\n\n```ts\nimport { DeferQueue } from '@poppinss/defer'\nconst queue = new DeferQueue()\n\nqueue.push(async function taskOne() {\n  console.log('The method will be called in the background')\n})\n\nconsole.log('Will be logged before the above log')\n```\n\nYou may also register a task as an object with `name` and `run` properties. The `name` is used for monitoring and the `run` method is the implementation callback.\n\n```ts\nqueue.push({\n  name: 'taskOne',\n  async run() {\n    console.log('The method will be called in the background')\n  },\n})\n```\n\n### Monitoring queue for completed tasks and errors\n\nSince the callback provided to the `queue.push` method runs in the background, there is no way to immediately await the callback to access the result or handle errors. Instead, you must use the `onError` and `taskCompleted` methods to monitor the queue.\n\n```ts\nqueue.onError = function (error, task) {\n  console.log(`${task.name} task failed with the following error`)\n  console.log(error)\n}\n\nqueue.taskCompleted = function (task) {\n  console.log(`${task.name} completed. ${queue.size()} tasks left`)\n}\n```\n\nYou may use the `queue.drained` method to get notified when the worker has processed the last task.\n\n```ts\nqueue.drained = function () {\n  console.log('Processed last task in the queue')\n}\n```\n\n### Pausing/resuming the queue\n\nYou may pause the queue from processing tasks using the `queue.pause` method and resume it using the `queue.resume` method.\n\n```ts\nqueue.pause()\n\nqueue.push(async function taskOne() {})\nqueue.push(async function taskTwo() {})\nqueue.push(async function taskThree() {})\n\nqueue.size() // 3\n\n// Start processing tasks\nqueue.resume()\n```\n\n### Killing the queue\n\nYou may remove all the tasks from the queue using the `queue.kill` method. This method removes all the pending tasks from the queue and invokes the `drain` handler.\n\n```ts\nprocess.on('beforeExit', () => {\n  queue.kill()\n})\n```\n\n### Concurrency\n\nBy default, 10 tasks are processed concurrently. However, you can specify the concurrency at the time of creating the queue instance.\n\n```ts\nconst queue = new DeferQueue({ concurrency: 4 })\n```\n\n## Testing\n\nWhen writing tests, you may want to immediately get notified when the queue has processed a task. You can do that by creating a monitoring promise before performing the task and `await` the promise after performing the task.\n\n```ts\nimport { DeferQueue } from '@poppinss/defer'\nexport const queue = new DeferQueue()\n\nexport function refreshCache() {\n  queue.push(async () => {\n    const users = await User.all()\n    cache.set('users', users)\n  })\n}\n```\n\nIn the following example, when we call the `refreshCache` method, we have no way of knowing when the underlying promise will be resolved, so we cannot write assertions.\n\n```ts\n// Test file\nimport { refreshCache } from './some-file.js'\n\nrefreshCache()\n// HOW DO WE KNOW IF THE CACHE WAS REFRESHED??\n```\n\nYou can linearly `await` the completion of a task using the `queue.createNotifier` method. The `createNotifier` method returns a promise that will be resolved after completing the task.\n\n```ts\nimport { queue, refreshCache } from './some-file.js'\n\nconst notifier = queue.createNotifier()\nrefreshCache()\n\nawait notifier\n/**\n * Task has been processed. You can now check if the\n * cache has been refreshed\n */\n```\n\n## Contributing\n\nOne of the primary goals of Poppinss is to have a vibrant community of users and contributors who believe in the principles of the framework.\n\nWe encourage you to read the [contribution guide](https://github.com/poppinss/.github/blob/main/docs/CONTRIBUTING.md) before contributing to the framework.\n\n## Code of Conduct\n\nIn order to ensure that the Poppinss community is welcoming to all, please review and abide by the [Code of Conduct](https://github.com/poppinss/.github/blob/main/docs/CODE_OF_CONDUCT.md).\n\n## License\n\nPoppinss defer is open-sourced software licensed under the [MIT license](LICENSE.md).\n\n[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/poppinss/defer/checks.yml?style=for-the-badge\n[gh-workflow-url]: https://github.com/poppinss/defer/actions/workflows/checks.yml 'Github action'\n[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript\n[typescript-url]: \"typescript\"\n[npm-image]: https://img.shields.io/npm/v/@poppinss/defer.svg?style=for-the-badge&logo=npm\n[npm-url]: https://npmjs.org/package/@poppinss/defer 'npm'\n[license-image]: https://img.shields.io/npm/l/@poppinss/defer?color=blueviolet&style=for-the-badge\n[license-url]: LICENSE.md 'license'\n","readmeFilename":"README.md"}