{"id":15365903,"url":"https://github.com/ivanprodaiko94/async-buffer","last_synced_at":"2025-09-12T08:41:00.073Z","repository":{"id":57131557,"uuid":"80943812","full_name":"IvanProdaiko94/Async-Buffer","owner":"IvanProdaiko94","description":"AsyncBuffer is used for async tasks accumulation and calling them sequentially after buffer limit will be exceeded. By default AsyncBuffer starts its operation automatically just after tasks limit is reached.","archived":false,"fork":false,"pushed_at":"2017-04-15T23:06:53.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T19:54:47.738Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IvanProdaiko94.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-04T19:45:30.000Z","updated_at":"2017-02-09T12:15:59.000Z","dependencies_parsed_at":"2022-09-01T00:12:47.820Z","dependency_job_id":null,"html_url":"https://github.com/IvanProdaiko94/Async-Buffer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanProdaiko94%2FAsync-Buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanProdaiko94%2FAsync-Buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanProdaiko94%2FAsync-Buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanProdaiko94%2FAsync-Buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IvanProdaiko94","download_url":"https://codeload.github.com/IvanProdaiko94/Async-Buffer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103911,"owners_count":21048245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T13:16:38.527Z","updated_at":"2025-04-09T19:54:52.495Z","avatar_url":"https://github.com/IvanProdaiko94.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"**AsyncBuffer** \n\nAsyncBuffer is used for async tasks accumulation and calling them sequentially after buffer limit will be exceeded.\nBy default AsyncBuffer starts its operation automatically just after tasks limit is reached.\nAsyncBuffer can be used in browser as well as in Node.\n\nFor example one can use this package to work with database.\nImagine you can use http server working and on each request you must push some info to database.\nInstead of doing it each time you can push task to buffer and writing to database will start after limit is exceeded.\n\nEach task is provided with callback as first parameter. It must be called to proceed operation.\nYou can optionally pass a parameter to the callback and it will be treated as a result of the task and stored inside `results` array.\nSecond parameter in task is result if previous task (if no parameter provided `null` will be pushed to `results` array).\nDefault stack capacity is 50 tasks.\n\nBasic usage:\n```javascript\nlet buffer = new AsyncBuffer(4),\n    task   = function(cb) {\n               setTimeout(function () {\n                   console.log(`I was called`);\n                   cb('result');\n               }, 1000)\n             };\n\nbuffer.on('drain', function (results) {\n    console.log('I was drained', results);\n}).push(task);\n```\n\nYou can switch off auto execution by setting second parameter in constructor to `false` and start task execution manually.\nAlso `push` method supports multiple parameters, so you can provide several tasks like so:\n```javascript\nlet buffer = new AsyncBuffer(5, false),\n    task = function(cb) {\n               setTimeout(function () {\n                   console.log(`I was called`);\n                   cb('result');\n               }, 1000)\n           };\nbuffer.on('stack_filled', function () {\n    console.log('Stack is filled');\n    buffer.drainBuffer();\n}).push(task, task, task, task, task);\n```\nor by using of `apply`\n```javascript\nbuffer.push(buffer, [task, task, task]);\n```\nAlso you there is two events that are used to notify about starting and ending of operation (`'start'` and `'drain'` respectively).\n`'drain'` event callback is provided with results of operation as a first parameter.\n```javascript\nbuffer.on('start', ()      =\u003e console.log('Execution is started'))\n      .on('drain', results =\u003e console.log('I was drained', results), \n                   results =\u003e console.log('Really drained', results));\n```\nAlso you can use `drainBufferParallel` method to replace sequential execution with parallel.\nAfter execution `chunk_done` event will be emitted and if no tasks were added during process `drain` event will be emitted as well.\n\nExample:\n```javascript\nbuffer.on('stop', () =\u003e console.log('stop'))\n      .push(task, task, task, task, task)\n      .drainBuffer()\n      .once('drain', res =\u003e {                           // wait till all sequential tasks will be executed;\n          console.log('drained', res);\n          buffer.push(task, task, task, task, task) \n                 .drainBufferParallel()                 // execute these tasks parallel;\n                 .push(task, task, task, task, task)    // add another chunk;\n                 .stopExecution()                       // stop execution (only first five tasks will be executed);\n                 .on('chunk_done', res =\u003e console.log('chunk_done', res)); // get results of first five tasks;\n      });\n```\n\nIf you need to:\n\n1. Start tasks execution before the limit will be exceeded or if second parameter in constructor was `false` use `drainBuffer` function.\n2. Clear tasks stack use `clearTasksStack` function.\n3. Stop execution at some moment of time use `stopExecution` function. Buffer will handle all tasks that was executed, but will not trigger next. Example:\n```javascript\nbuffer.on('stop', function(currentResults) {\n    console.log(`Execution has been stopped. Here is results ${currentResults}`);\n    //continue execution\n    buffer.drainBuffer();\n})\n```\n4. Drain buffer before process will exit you can use monkey patch provided in this package like that:\n```javascript\nmonkeyPatch(function (exit) {\n    return function () {\n        buffer.on('drain', () =\u003e exit())\n              .drainBuffer();\n    }\n});\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanprodaiko94%2Fasync-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivanprodaiko94%2Fasync-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanprodaiko94%2Fasync-buffer/lists"}