{"id":13431207,"url":"https://github.com/sindresorhus/p-wait-for","last_synced_at":"2025-04-08T17:17:52.397Z","repository":{"id":12313361,"uuid":"71538128","full_name":"sindresorhus/p-wait-for","owner":"sindresorhus","description":"Wait for a condition to be true","archived":false,"fork":false,"pushed_at":"2023-06-11T11:18:05.000Z","size":33,"stargazers_count":158,"open_issues_count":1,"forks_count":19,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-09T18:32:57.475Z","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":"2016-10-21T06:52:18.000Z","updated_at":"2025-03-09T01:29:18.000Z","dependencies_parsed_at":"2023-02-17T05:15:17.896Z","dependency_job_id":"449fa249-8408-4f57-907c-281633650e2c","html_url":"https://github.com/sindresorhus/p-wait-for","commit_stats":{"total_commits":35,"total_committers":9,"mean_commits":3.888888888888889,"dds":0.4,"last_synced_commit":"e14fd133fd15b446a87d8f4655cfbe8236de8044"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-wait-for","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-wait-for/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-wait-for/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-wait-for/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/p-wait-for/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242811944,"owners_count":20189162,"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-31T02:01:01.329Z","updated_at":"2025-03-11T11:12:13.773Z","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":["JavaScript","Packages","Convenience Utilities"],"sub_categories":["sindresorhus's many Promise utilities ([see notes](https://github.com/sindresorhus/promise-fun))","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)))"],"readme":"# p-wait-for\n\n\u003e Wait for a condition to be true\n\nCan be useful for polling.\n\n## Install\n\n```sh\nnpm install p-wait-for\n```\n\n## Usage\n\n```js\nimport pWaitFor from 'p-wait-for';\nimport {pathExists} from 'path-exists';\n\nawait pWaitFor(() =\u003e pathExists('unicorn.png'));\nconsole.log('Yay! The file now exists.');\n```\n\n## API\n\n### pWaitFor(condition, options?)\n\nReturns a `Promise` that resolves when `condition` returns `true`. Rejects if `condition` throws or returns a `Promise` that rejects.\n\n#### condition\n\nType: `Function`\n\nExpected to return `Promise\u003cboolean\u003e | boolean`.\n\n#### options\n\nType: `object`\n\n##### interval\n\nType: `number`\\\nDefault: `20`\n\nNumber of milliseconds to wait after `condition` resolves to `false` before calling it again.\n\n##### timeout\n\nType: `number | TimeoutOptions`\\\nDefault: `Infinity`\n\nNumber of milliseconds to wait before automatically rejecting with a `TimeoutError`.\n\nYou can customize the timeout `Error` by specifying `TimeoutOptions`.\n\n```js\nimport pWaitFor from 'p-wait-for';\nimport {pathExists} from 'path-exists';\n\nconst originalSetTimeout = setTimeout;\nconst originalClearTimeout = clearTimeout;\n\nsinon.useFakeTimers();\n\nawait pWaitFor(() =\u003e pathExists('unicorn.png'), {\n\ttimeout: {\n\t\tmilliseconds: 100,\n\t\tmessage: new MyError('Time’s up!'),\n\t\tcustomTimers: {\n\t\t\tsetTimeout: originalSetTimeout,\n\t\t\tclearTimeout: originalClearTimeout\n\t\t}\n\t}\n});\n\nconsole.log('Yay! The file now exists.');\n```\n\n###### milliseconds\n\nType: `number`\\\nDefault: `Infinity`\n\nMilliseconds before timing out.\n\nPassing `Infinity` will cause it to never time out.\n\n###### message\n\nType: `string | Error`\nDefault: `'Promise timed out after 50 milliseconds'`\n\nSpecify a custom error message or error.\n\nIf you do a custom error, it's recommended to sub-class `TimeoutError`.\n\n###### customTimers\n\nType: `object` with function properties `setTimeout` and `clearTimeout`\n\nCustom implementations for the `setTimeout` and `clearTimeout` functions.\n\nUseful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/).\n\n###### fallback\n\nType: `Function`\n\nDo something other than rejecting with an error on timeout.\n\nExample:\n\n```js\nimport pWaitFor from 'p-wait-for';\nimport {pathExists} from 'path-exists';\n\nawait pWaitFor(() =\u003e pathExists('unicorn.png'), {\n\ttimeout: {\n\t\tmilliseconds: 50,\n\t\tfallback: () =\u003e {\n\t\t\tconsole.log('Time’s up! executed the fallback function!');\n\t\t},\n\t}\n});\n```\n\n##### before\n\nType: `boolean`\\\nDefault: `true`\n\nWhether to run the check immediately rather than starting by waiting `interval` milliseconds.\n\nUseful for when the check, if run immediately, would likely return `false`. In this scenario, set `before` to `false`.\n\n#### resolveWith(value)\n\nResolve the main promise with a custom value.\n\n```js\nimport pWaitFor from 'p-wait-for';\nimport pathExists from 'path-exists';\n\nconst path = await pWaitFor(async () =\u003e {\n\tconst path = getPath();\n\treturn await pathExists(path) \u0026\u0026 pWaitFor.resolveWith(path);\n});\n\nconsole.log(path);\n```\n\n### TimeoutError\n\nExposed for instance checking.\n\n## Related\n\n- [p-whilst](https://github.com/sindresorhus/p-whilst) - Calls a function repeatedly while a condition returns true and then resolves the promise\n- [More…](https://github.com/sindresorhus/promise-fun)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fp-wait-for","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fp-wait-for","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fp-wait-for/lists"}