{"id":16822698,"url":"https://github.com/jlipps/asyncbox","last_synced_at":"2025-05-12T17:23:07.946Z","repository":{"id":24171059,"uuid":"27561476","full_name":"jlipps/asyncbox","owner":"jlipps","description":"A collection of ES7 async/await utilities","archived":false,"fork":false,"pushed_at":"2024-07-02T18:24:59.000Z","size":138,"stargazers_count":25,"open_issues_count":2,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-12T17:22:57.868Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jlipps.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-12-04T21:44:03.000Z","updated_at":"2024-11-14T03:09:43.000Z","dependencies_parsed_at":"2024-01-12T00:27:05.471Z","dependency_job_id":"610470ea-34d4-4589-9ab6-52cfeae734ae","html_url":"https://github.com/jlipps/asyncbox","commit_stats":{"total_commits":83,"total_committers":11,"mean_commits":7.545454545454546,"dds":0.3012048192771084,"last_synced_commit":"d0adc2145673c66c1b64c83739dfcaef4f59d3e1"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlipps%2Fasyncbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlipps%2Fasyncbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlipps%2Fasyncbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlipps%2Fasyncbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jlipps","download_url":"https://codeload.github.com/jlipps/asyncbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253785078,"owners_count":21963914,"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-13T11:05:29.976Z","updated_at":"2025-05-12T17:23:07.918Z","avatar_url":"https://github.com/jlipps.png","language":"JavaScript","readme":"asyncbox\n========\n\nA collection of ES7 async/await utilities. Install via NPM:\n\n```\nnpm install asyncbox\n```\n\nThen, behold!\n\n### Sleep\n\nAn async/await version of setTimeout\n\n```js\nimport { sleep } from 'asyncbox';\n\nasync function myFn () {\n    // do some stuff\n    await sleep(1000); // wait one second\n    // do some other stuff\n};\n```\n\n### Long Sleep\n\nSometimes `Promise.delay` or `setTimeout` are inaccurate for large wait times. To safely wait for these long times (e.g. in the 5+ minute range), you can use `longSleep`:\n\n```js\nimport { longSleep } from 'asyncbox';\n\nasync function myFn () {\n  await longSleep(10 * 60 * 1000); // wait for 10 mins\n  await longSleep(5000, {thresholdMs: 10000}); // wait for 5s. Anything below the thresholdMs will use a single sleep\n  await longSleep(5000, {intervalMs: 500}); // check the clock every 500ms to see if waiting should stop\n}\n```\n\nYou can also pass a `progressCb` option which is a callback function that receives an object with the properties `elapsedMs`, `timeLeft`, and `progress`. This will be called on every wait interval so you can do your wait logging or whatever.\n\n```js\nfunction progressCb({elapsedMs, timeLeft, progress}) {\n  console.log(`We are {progress * 100}% complete waiting`);\n}\nawait longSleep(10 * 60 * 1000, {progressCb});\n```\n\n### Retry\n\nAn async/await way of running a method until it doesn't throw an error\n\n```js\nimport { sleep, retry } from 'asyncbox';\n\nasync function flakeyFunction (val1, val2) {\n    if (val1 \u003c 10) {\n        throw new Error(\"this is a flakey value\");\n    }\n    await sleep(1000);\n    return val1 + val2;\n}\n\nasync function myFn () {\n    let randVals = [Math.random() * 100, Math.random() * 100];\n\n    // run flakeyFunction up to 3 times until it succeeds.\n    // if it doesn't, we'll get the error thrown in this context\n    let randSum = await retry(3, flakeyFunction, ...randVals);\n}\n```\n\nYou can also use `retryInterval` to add a sleep in between retries. This can be\nuseful if you want to throttle how fast we retry:\n\n```js\nawait retryInterval(3, 1500, expensiveFunction, ...args);\n```\n\n### Filter/Map\n\nFilter and map are pretty handy concepts, and now you can write filter and map\nfunctions that execute asynchronously!\n\n```js\nimport { asyncmap, asyncfilter } from 'asyncbox';\n```\n\nThen in your async functions, you can do:\n\n```js\nconst items = [1, 2, 3, 4];\nconst slowSquare = async (n) =\u003e { await sleep(5); return n * 2; };\nlet newItems = await asyncmap(items, async (i) =\u003e { return await slowSquare(i); });\nconsole.log(newItems);  // [1, 4, 9, 16];\n\nconst slowEven = async (n) =\u003e { await sleep(5); return n % 2 === 0; };\nnewItems = await asyncfilter(items, async (i) =\u003e { return await slowEven(i); });\nconsole.log(newItems); // [2, 4];\n```\n\nBy default, `asyncmap` and `asyncfilter` run their operations in parallel; you\ncan pass `false` as a third argument to make sure it happens serially.\n\n### Nodeify\n\nExport async functions (Promises) and import this with your ES5 code to use it\nwith Node.\n\n```js\nvar asyncbox = require('asyncbox')\n  , sleep = asyncbox.sleep\n  , nodeify = asyncbox.nodeify;\n\nnodeify(sleep(1000), function (err, timer) {\n  console.log(err); // null\n  console.log(timer); // timer obj\n});\n```\n\n### nodeifyAll\n\nIf you have a whole library you want to export nodeified versions of, it's pretty easy:\n\n```js\nimport { nodeifyAll } from 'asyncbox';\n\nasync function foo () { ... }\nasync function bar () { ... }\nlet cb = nodeifyAll({foo, bar});\nexport { foo, bar, cb };\n```\n\nThen in my ES5 script I can do:\n\n```js\nvar myLib = require('mylib').cb;\n\nmyLib.foo(function (err) { ... });\nmyLib.bar(function (err) { ... });\n```\n\n### waitForCondition\n\nTakes a condition (a function returning a boolean or boolean promise),\nand waits until the condition is true.\n\nThrows a `/Condition unmet/` error if the condition has not been\nsatisfied within the allocated time, unless an error is provided in\nthe options, as the `error` property, which is either thrown itself, or\nused as the message.\n\nThe condition result is returned if it is not falsy. If the condition\nthrows an error then this exception will be immediately passed through.\n\nThe default options are: `{ waitMs: 5000, intervalMs: 500 }`\n\n```js\n// define your own condition\nfunction condFn () { return Math.random()*1000 \u003e 995; }\n\n// with default params\nawait waitForCondition(condFn);\n\n// with options\nawait waitForCondition(condFn, {\n  waitMs: 300000,\n  intervalMs: 10000\n});\n\n// pass a logger to get extra debug info\nawait waitForCondition(condFn, {\n  waitMs: 300000,\n  intervalMs: 10000\n  logger: myLogger // expects a debug method\n});\n\n// pass an error string to get that message in the resulting exception\ntry {\n  await waitForCondition(condFn, {\n    error: 'Unable to satisfy condition'\n  });\n} catch (err) {\n  // err.message === 'Unable to satisfy condition'\n}\n\n// pass an error instance to be thrown\nconst error = new Error('Unable to satisfy condition');\ntry {\n  await waitForCondition(condFn, {\n    error: error\n  });\n} catch (err) {\n  // err === error\n}\n```\n\n### Run the tests\n\n```\nnpm test\n```\n","funding_links":[],"categories":["Libraries"],"sub_categories":["[Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlipps%2Fasyncbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjlipps%2Fasyncbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlipps%2Fasyncbox/lists"}