{"id":13454444,"url":"https://github.com/sindresorhus/delay","last_synced_at":"2025-05-14T19:08:05.254Z","repository":{"id":44404602,"uuid":"46072466","full_name":"sindresorhus/delay","owner":"sindresorhus","description":"Delay a promise a specified amount of time","archived":false,"fork":false,"pushed_at":"2023-06-14T08:28:50.000Z","size":59,"stargazers_count":614,"open_issues_count":1,"forks_count":36,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-05-10T17:23:22.332Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2015-11-12T18:31:41.000Z","updated_at":"2025-04-06T07:38:58.000Z","dependencies_parsed_at":"2023-09-24T03:33:09.446Z","dependency_job_id":null,"html_url":"https://github.com/sindresorhus/delay","commit_stats":{"total_commits":65,"total_committers":17,"mean_commits":3.823529411764706,"dds":"0.36923076923076925","last_synced_commit":"6779d5ac90a34ec692a07389c11649e66e8a92a7"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdelay","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdelay/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdelay/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdelay/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/delay/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254209859,"owners_count":22032897,"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-07-31T08:00:54.127Z","updated_at":"2025-05-14T19:08:02.894Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["Packages","JavaScript","Repository","包","Control flow","Works with AVA","Utilities","目录","Convenience Utilities"],"sub_categories":["Control flow","流程控制","React Components","Control flow Promises","控制流","sindresorhus's many Promise utilities (\u003cb\u003e\u003ccode\u003e\u0026nbsp;\u0026nbsp;5146⭐\u003c/code\u003e\u003c/b\u003e \u003cb\u003e\u003ccode\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;138🍴\u003c/code\u003e\u003c/b\u003e [see notes](https://github.com/sindresorhus/promise-fun)))","sindresorhus's many Promise utilities ([see notes](https://github.com/sindresorhus/promise-fun))"],"readme":"# delay\n\n\u003e Delay a promise a specified amount of time\n\n*If you target Node.js only, you can use `import {setTimeout} from 'node:timers/promises'; await setTimeout(1000);` instead. This package can still be useful if you need browser support or the extra features.*\n\n## Install\n\n```sh\nnpm install delay\n```\n\n## Usage\n\n```js\nimport delay from 'delay';\n\nbar();\n\nawait delay(100);\n\n// Executed 100 milliseconds later\nbaz();\n```\n\n## API\n\n### delay(milliseconds, options?) \u003csup\u003edefault import\u003c/sup\u003e\n\nCreate a promise which resolves after the specified `milliseconds`.\n\n### rangeDelay(minimum, maximum, options?)\n\nCreate a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.\n\nUseful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with this method, you could give it a threshold instead.\n\n#### milliseconds\n#### mininum\n#### maximum\n\nType: `number`\n\nMilliseconds to delay the promise.\n\n#### options\n\nType: `object`\n\n##### value\n\nType: `unknown`\n\nA value to resolve in the returned promise.\n\n```js\nimport delay from 'delay';\n\nconst result = await delay(100, {value: '🦄'});\n\n// Executed after 100 milliseconds\nconsole.log(result);\n//=\u003e '🦄'\n```\n\n##### signal\n\nType: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)\n\nThe returned promise will be rejected with an `AbortError` if the signal is aborted.\n\n```js\nimport delay from 'delay';\n\nconst abortController = new AbortController();\n\nsetTimeout(() =\u003e {\n\tabortController.abort();\n}, 500);\n\ntry {\n\tawait delay(1000, {signal: abortController.signal});\n} catch (error) {\n\t// 500 milliseconds later\n\tconsole.log(error.name)\n\t//=\u003e 'AbortError'\n}\n```\n\n### clearDelay(delayPromise)\n\nClears the delay and settles the promise.\n\nIf you pass in a promise that is already cleared or a promise coming from somewhere else, it does nothing.\n\n```js\nimport delay, {clearDelay} from 'delay';\n\nconst delayedPromise = delay(1000, {value: 'Done'});\n\nsetTimeout(() =\u003e {\n\tclearDelay(delayedPromise);\n}, 500);\n\n// 500 milliseconds later\nconsole.log(await delayedPromise);\n//=\u003e 'Done'\n```\n\n### createDelay({clearTimeout, setTimeout})\n\nCreates a new `delay` instance using the provided functions for clearing and setting timeouts. Useful if you're about to stub timers globally, but you still want to use `delay` to manage your tests.\n\n```js\nimport {createDelay} from 'delay';\n\nconst customDelay = createDelay({clearTimeout, setTimeout});\n\nconst result = await customDelay(100, {value: '🦄'});\n\n// Executed after 100 milliseconds\nconsole.log(result);\n//=\u003e '🦄'\n```\n\n## Related\n\n- [delay-cli](https://github.com/sindresorhus/delay-cli) - CLI for this module\n- [p-cancelable](https://github.com/sindresorhus/p-cancelable) - Create a promise that can be canceled\n- [p-min-delay](https://github.com/sindresorhus/p-min-delay) - Delay a promise a minimum amount of time\n- [p-immediate](https://github.com/sindresorhus/p-immediate) - Returns a promise resolved in the next event loop - think `setImmediate()`\n- [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a specified amount of time\n- [More…](https://github.com/sindresorhus/promise-fun)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fdelay","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fdelay","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fdelay/lists"}