{"_id":"p-fifo","_rev":"4576705","name":"p-fifo","description":"Promised First-In-First-Out buffer. Await on push to be told when a value is consumed and await on shift for a value to consume when the buffer is empty.","dist-tags":{"latest":"1.0.0"},"maintainers":[{"name":"alanshaw","email":""}],"time":{"modified":"2026-04-10T15:45:03.000Z","created":"2019-07-31T13:55:48.044Z","1.0.0":"2019-07-31T13:55:48.044Z"},"users":{},"author":{"name":"Alan Shaw"},"repository":{"type":"git","url":"git+https://github.com/alanshaw/p-fifo.git"},"versions":{"1.0.0":{"name":"p-fifo","version":"1.0.0","description":"Promised First-In-First-Out buffer. Await on push to be told when a value is consumed and await on shift for a value to consume when the buffer is empty.","main":"index.js","scripts":{"test":"ava test.js --verbose","lint":"standard","coverage":"nyc --reporter=text --reporter=lcov npm test"},"keywords":["fifo","buffer","queue","promise","push","shift","array"],"author":{"name":"Alan Shaw"},"license":"MIT","dependencies":{"fast-fifo":"^1.0.0","p-defer":"^3.0.0"},"devDependencies":{"ava":"^2.2.0","nyc":"^14.1.1","standard":"^13.1.0"},"ava":{"babel":false,"compileEnhancements":false},"repository":{"type":"git","url":"git+https://github.com/alanshaw/p-fifo.git"},"bugs":{"url":"https://github.com/alanshaw/p-fifo/issues"},"homepage":"https://github.com/alanshaw/p-fifo#readme","gitHead":"a1d5bd029a7daad02182d6532fbc5a6a2c3caa13","_id":"p-fifo@1.0.0","_nodeVersion":"12.2.0","_npmVersion":"6.9.0","_npmUser":{"name":"alanshaw","email":"alan138@gmail.com"},"dist":{"shasum":"e29d5cf17c239ba87f51dde98c1d26a9cfe20a63","size":2829,"noattachment":false,"key":"/p-fifo/-/p-fifo-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/p-fifo/download/p-fifo-1.0.0.tgz"},"maintainers":[{"name":"alanshaw","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-fifo_1.0.0_1564581347700_0.7020078385258748"},"_hasShrinkwrap":false,"publish_time":1564581348044,"_cnpm_publish_time":1564581348044,"_cnpmcore_publish_time":"2021-12-16T13:36:08.447Z"}},"readme":"# p-fifo\n\n[![Build Status](https://travis-ci.org/alanshaw/p-fifo.svg?branch=master)](https://travis-ci.org/alanshaw/p-fifo)\n[![dependencies Status](https://david-dm.org/alanshaw/p-fifo/status.svg)](https://david-dm.org/alanshaw/p-fifo)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n> Promised First-In-First-Out buffer. Await on push to be told when a value is consumed and await on shift for a value to consume when the buffer is empty\n\n## Install\n\n```sh\nnpm i p-fifo\n```\n\n## Usage\n\n### Await on push\n\n`await` on push to be told when your pushed value is consumed:\n\n```js\nconst Fifo = require('p-fifo')\nconst fifo = new Fifo()\n\n// Consume a value from the buffer after 1 second\nsetTimeout(() => fifo.shift(), 1000)\n\nconsole.time('push')\n// Nothing in the buffer, push a value and wait for it to be consumed\nawait fifo.push('hello')\nconsole.log('\"hello\" was consumed')\nconsole.timeEnd('push')\n\n// Output:\n// \"hello\" was consumed\n// push: 1006.723ms\n```\n\n### Await on shift\n\nIf the buffer is empty, you can `await` on a value to be pushed:\n\n```js\nconst Fifo = require('p-fifo')\nconst fifo = new Fifo()\n\n// Push a value into the buffer after 1 second\nsetTimeout(() => fifo.push('hello'), 1000)\n\nconsole.time('shift')\n// Nothing in the buffer, wait for something to arrive\nconst value = await fifo.shift()\nconsole.log(`consumed \"${value}\" from the buffer`)\nconsole.timeEnd('shift')\n\n// Output:\n// consumed \"hello\" from the buffer\n// shift: 1002.652ms\n```\n\n## API\n\n```js\nconst fifo = new Fifo()\n```\n\n### `fifo.push(value): Promise`\n\nAdd a value to the end of the FIFO buffer.\n\nReturns a promise that is resolved when the pushed value is shifted off the start of the buffer.\n\n### `fifo.shift(): Promise<Any>`\n\nRemove the first value from the FIFO buffer and return that removed value in a promise.\n\nReturns a promise that resolves to a value from start of the FIFO buffer. If there are no values in the buffer the promise will resolve when a value is next pushed.\n\nNote that multiple calls to shift when the buffer is empty will not resolve to the same value i.e. a corresponding number of calls to `push` will need to be made to resolve all the promises returned by calls to `shift`.\n\n### `fifo.isEmpty(): Boolean`\n\nReturns `true` if the FIFO buffer is empty and `false` otherwise.\n\n## Contribute\n\nFeel free to dive in! [Open an issue](https://github.com/alanshaw/p-fifo/issues/new) or submit PRs.\n\n## License\n\n[MIT](LICENSE) © Alan Shaw\n","_attachments":{},"homepage":"https://github.com/alanshaw/p-fifo#readme","bugs":{"url":"https://github.com/alanshaw/p-fifo/issues"},"license":"MIT"}