{"id":26709402,"url":"https://github.com/js-bits/timeout","last_synced_at":"2025-07-28T12:03:06.799Z","repository":{"id":57122350,"uuid":"356738120","full_name":"js-bits/timeout","owner":"js-bits","description":"Promise-based timeout","archived":false,"fork":false,"pushed_at":"2023-07-19T02:06:34.000Z","size":1032,"stargazers_count":3,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T08:16:44.487Z","etag":null,"topics":["javascript","promise","timeout"],"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/js-bits.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":"2021-04-11T01:22:07.000Z","updated_at":"2023-07-03T17:56:33.000Z","dependencies_parsed_at":"2022-08-24T14:59:27.764Z","dependency_job_id":null,"html_url":"https://github.com/js-bits/timeout","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Ftimeout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Ftimeout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Ftimeout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Ftimeout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/js-bits","download_url":"https://codeload.github.com/js-bits/timeout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248752382,"owners_count":21156081,"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":["javascript","promise","timeout"],"created_at":"2025-03-27T08:16:47.540Z","updated_at":"2025-04-13T17:32:49.381Z","avatar_url":"https://github.com/js-bits.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Promise-based timeout\n\nA convenient, promise-based way to work with timeouts. It's useful to track asynchronous operations (HTTP requests for instance) latency.\n\n## Installation\n\nInstall with npm:\n\n```\nnpm install @js-bits/timeout\n```\n\nInstall with yarn:\n\n```\nyarn add @js-bits/timeout\n```\n\nImport where you need it:\n\n```javascript\nimport Timeout from '@js-bits/timeout';\n```\n\nor require for CommonJS:\n\n```javascript\nconst Timeout = require('@js-bits/timeout');\n```\n\n## How to use\n\nBasic approach:\n\n```javascript\nconst timeout = new Timeout(1000); // 1 sec\n\ntimeout.set().catch(() =\u003e {\n  console.log('Timeout exceeded');\n});\n```\n\nAlternative approach:\n\n```javascript\nconst timeout = new Timeout(2000); // 2 sec\n\ntimeout.catch(() =\u003e {\n  console.log('Timeout exceeded');\n});\n\n// ...\n\ntimeout.set();\n```\n\nError handling:\n\n```javascript\nconst timeout = new Timeout(3000); // 3 sec\n\ntimeout.set().catch(reason =\u003e {\n  if (reason.name === Timeout.TimeoutExceededError) {\n    console.log('Timeout exceeded error');\n  }\n});\n```\n\nActual usage:\n\n```javascript\nconst timeout = new Timeout(1000); // 1 sec\n\ntimeout.catch(() =\u003e {\n  // you can report the exceeded timeout here\n  console.log('Asynchronous operation timeout exceeded');\n});\n\n// fake async operation\nconst asyncAction = async delay =\u003e\n  new Promise(resolve =\u003e {\n    setTimeout(resolve, delay);\n  });\n\n(async () =\u003e {\n  // at the beginning of the operation\n  timeout.set();\n\n  // perform some asynchronous actions which could potentially\n  // take more time than the specified timeout\n  const asyncActionPromise = asyncAction(2000); // 2 sec\n\n  timeout.catch(() =\u003e {\n    // you can also report the exceeded timeout or abort the operation here\n  });\n\n  await asyncActionPromise;\n\n  // when the operation is completed\n  timeout.clear();\n})();\n```\n\n## Notes\n\n- You cannot \"pause\" a timeout or \"reset\" it. Once it's set, there are only two possibilities: either the timeout can be manually cleared before it is exceeded or the timeout will be exceeded.\n- It's possible to clear a timeout before it is even set up but you won't be able to set that timeout up ever again.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-bits%2Ftimeout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjs-bits%2Ftimeout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-bits%2Ftimeout/lists"}