{"id":15683807,"url":"https://github.com/magicdawn/promise.timeout","last_synced_at":"2025-05-07T14:25:32.054Z","repository":{"id":57331555,"uuid":"58720077","full_name":"magicdawn/promise.timeout","owner":"magicdawn","description":"add timeout support for async function","archived":false,"fork":false,"pushed_at":"2024-01-24T07:56:25.000Z","size":157,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-07T14:25:30.442Z","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/magicdawn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2016-05-13T08:51:46.000Z","updated_at":"2024-07-24T21:43:51.000Z","dependencies_parsed_at":"2024-01-24T08:50:05.625Z","dependency_job_id":null,"html_url":"https://github.com/magicdawn/promise.timeout","commit_stats":{"total_commits":65,"total_committers":1,"mean_commits":65.0,"dds":0.0,"last_synced_commit":"fac9e333ffdc1da434a300be3f4b0d14ca7f3e21"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.timeout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.timeout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.timeout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.timeout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magicdawn","download_url":"https://codeload.github.com/magicdawn/promise.timeout/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252893672,"owners_count":21820860,"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-03T17:08:42.854Z","updated_at":"2025-05-07T14:25:32.033Z","avatar_url":"https://github.com/magicdawn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- AUTO_GENERATED_UNTOUCHED_FLAG --\u003e\n\n# promise.timeout\n\n\u003e add timeout support for async function\n\n[![Build Status](https://img.shields.io/github/actions/workflow/status/magicdawn/promise.timeout/ci.yml?style=flat-square\u0026branch=main)](https://github.com/magicdawn/promise.timeout/actions/workflows/ci.yml)\n[![Coverage Status](https://img.shields.io/codecov/c/github/magicdawn/promise.timeout.svg?style=flat-square)](https://codecov.io/gh/magicdawn/promise.timeout)\n[![npm version](https://img.shields.io/npm/v/promise.timeout.svg?style=flat-square)](https://www.npmjs.com/package/promise.timeout)\n[![npm downloads](https://img.shields.io/npm/dm/promise.timeout.svg?style=flat-square)](https://www.npmjs.com/package/promise.timeout)\n[![npm license](https://img.shields.io/npm/l/promise.timeout.svg?style=flat-square)](http://magicdawn.mit-license.org)\n\n## Install\n\n```sh\n$ pnpm add promise.timeout\n```\n\n## Note\n\n- this module targets ES5 environment.\n- this module require global `AbortController`, Node.js has builtin global since v15\n\n## API\n\n```js\nvar ptimeout = require('promise.timeout')\n```\n\n`ptimeout(fn, timeout)`\n\n- `fn` the async function\n- `timeout` in ms\n\n```js\nvar ptimeout = require('promise.timeout')\n\n// a function will cost 20ms\nfunction test() {\n  return new Promise(function (resolve, reject) {\n    setTimeout(function () {\n      resolve(20)\n    }, 20)\n  })\n}\n\nvar test10 = ptimeout(test, 10)\nvar test50 = ptimeout(test, 50)\n\n// 10 timeout\ntry {\n  await test10()\n} catch (e) {\n  e.should.be.ok()\n  e.should.be.instanceof(ptimeout.TimeoutError)\n  e.message.should.match(/timeout/)\n  e.timeout.should.equal(10)\n}\n\n// 50 ok\nvar _50 = await test50()\n_50.should.be.ok()\n_50.should.equal(20)\n```\n\n### singal\n\nptimeout will provide an extra runtime argument `signal: AbortSignal`, you can use the signal to register abort action.\nwhen timeout reached, the abort action will be executed\n\n```js\nvar ptimeout = require('promise.timeout')\n\n// a function will cost 20ms\nfunction test(signal) {\n  return new Promise(function (resolve, reject) {\n    var timer = setTimeout(function () {\n      resolve(20)\n    }, 20)\n\n    // clean up\n    signal.addEventListener('abort', () =\u003e {\n      clearTimeout(timer)\n    })\n  })\n}\n\nvar test10 = ptimeout(test, 10)\ntry {\n  await test10()\n} catch (e) {\n  e.should.ok()\n}\n```\n\n## FAQ\n\n### Q: why move to `AbortController`\n\n### A:\n\n- easy to type, easy to strip signal argument, easy to use with TypeScript\n- AND it's shiped in Node.js https://nodejs.org/api/globals.html#class-abortcontroller\n- for browser, users should consider a polyfill for `AbortController` \u0026 `AbortSignal` if not provided nativly\n\n\u003cdetails\u003e\u003csummary\u003eold version use `onCancel` to register clean up action\u003c/summary\u003e\n\n### Q: \u003cdel\u003eWhy onCancel\u003c/del\u003e\n\n### A: Think `onCancel` like the AbortController\n\nwith `AbortController` you need to\n\n```js\nfunction normalFn(a, r, g, s, controller: AbortController) {\n  controller.signal.addEventListener('abort', () =\u003e {\n    // cancel operations that starts in `normalFn` body\n  })\n}\n```\n\n- and `ptimeout` will call the `controller.abort()` if any timeout exceeds\n- and with `onCancel`, you provide a cancel operation to ptimeout, ptimeout will call that\n\nThat's the same, and I don't want to depend on an extra package [abort-controller](https://github.com/mysticatea/abort-controller)\n\n\u003c/details\u003e\n\n## See Also\n\n- [promise.timeout](https://github.com/magicdawn/promise.timeout)\n- [promise.retry](https://github.com/magicdawn/promise.retry)\n- [promise.map](https://github.com/magicdawn/promise.map)\n- [promise.ify](https://github.com/magicdawn/promise.ify)\n- [promise.cb](https://github.com/magicdawn/promise.cb)\n- [promise.obj](https://github.com/magicdawn/promise.obj)\n- [promise.sleep](https://github.com/magicdawn/promise.sleep)\n\n## Changelog\n\n[CHANGELOG.md](CHANGELOG.md)\n\n## License\n\nthe MIT License http://magicdawn.mit-license.org\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicdawn%2Fpromise.timeout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagicdawn%2Fpromise.timeout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicdawn%2Fpromise.timeout/lists"}