{"id":13672917,"url":"https://github.com/huruji/p-first","last_synced_at":"2025-03-18T06:43:35.022Z","repository":{"id":57317396,"uuid":"184189429","full_name":"huruji/p-first","owner":"huruji","description":"make your Promise support getting first fulfilled Promise ","archived":false,"fork":false,"pushed_at":"2019-04-30T07:32:52.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-25T02:06:54.497Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/huruji.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}},"created_at":"2019-04-30T04:05:19.000Z","updated_at":"2019-05-01T09:00:17.000Z","dependencies_parsed_at":"2022-08-25T21:11:48.754Z","dependency_job_id":null,"html_url":"https://github.com/huruji/p-first","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fp-first","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fp-first/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fp-first/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huruji%2Fp-first/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huruji","download_url":"https://codeload.github.com/huruji/p-first/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244173500,"owners_count":20410295,"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-08-02T09:01:57.188Z","updated_at":"2025-03-18T06:43:34.995Z","avatar_url":"https://github.com/huruji.png","language":"TypeScript","readme":"中文 | [English](./README_EN.md)\n\n# p-first\n\n让你的 Promise 支持获取第一个变为 fulfilled 状态的函数\n\n\u003e 不同于 `Promise.race` 不管是 fulfilled 或者是 rejected 状态都会返回\n\n## How to use\n\n```bash\nnpm i p-first -S\n```\n\n```js\nimport first from 'p-first'\n\nfirst(yourPromises)\n```\n\n或者你可以拓展你的 `Promise` 构造函数\n\n```js\nPromise.first = first\n```\n\n## real world use\n\n你有两个 Ajax api 获取相同的数据，但是你不知道哪个 Ajax api 会先返回，也无法预测哪个 Ajax api 会出问题, 你可以使用 `p-first` 来获取**第一个成功返回**的数据\n\n```js\nconst p1 = new Promise(resolve =\u003e {\n    setTimeout(() =\u003e {\n        resolve (12)\n    }, 5000)\n})\n\nconst p2 = new Promise(resolve =\u003e {\n    setTimeout (() =\u003e {\n        resolve ('huruji')\n    }, 1000)\n})\n\nconst p3 = new Promise((resolve, reject) =\u003e {\n    reject(404)\n})\n\nPromise.first([p1, p3, p2]).then(data =\u003e {\n    console.log(data)\n})\n\n// output: huruji\n```\n\n## catch error\n\n当没有一个 Promise 变为了 fulfilled 状态，你可以在 `.catch` 中捕获 error\n\n```js\nconst p1 = new Promise((resolve, reject) =\u003e {\n    setTimeout(() =\u003e {\n        reject (12)\n    }, 5000)\n})\n\nconst p2 = new Promise((resolve, reject) =\u003e {\n    setTimeout (() =\u003e {\n        reject('huruji')\n    }, 1000)\n})\n\nconst p3 = new Promise((resolve, reject) =\u003e {\n    reject(404)\n})\n\nPromise.first([p1, p3, p2]).then(data =\u003e {\n    console.log(data)\n}).catch((errs) =\u003e {\n    console.log('have errors')\n    console.log(errs)\n})\n\n// have errors\n// [12, 404, 'huruji']\n```","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuruji%2Fp-first","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuruji%2Fp-first","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuruji%2Fp-first/lists"}