{"id":30261592,"url":"https://github.com/zcfan/p-task","last_synced_at":"2026-01-20T17:29:32.972Z","repository":{"id":306851296,"uuid":"1026063512","full_name":"zcfan/p-task","owner":"zcfan","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-28T03:22:55.000Z","size":47,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-09T00:44:40.436Z","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/zcfan.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-25T08:48:11.000Z","updated_at":"2025-07-28T03:22:58.000Z","dependencies_parsed_at":"2025-07-28T03:10:23.740Z","dependency_job_id":"523714e5-a457-4e13-ac10-c3b9f94e0d0d","html_url":"https://github.com/zcfan/p-task","commit_stats":null,"previous_names":["zcfan/p-task"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zcfan/p-task","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcfan%2Fp-task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcfan%2Fp-task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcfan%2Fp-task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcfan%2Fp-task/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zcfan","download_url":"https://codeload.github.com/zcfan/p-task/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcfan%2Fp-task/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270624824,"owners_count":24618265,"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","status":"online","status_checked_at":"2025-08-15T02:00:12.559Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-08-15T20:08:29.835Z","updated_at":"2026-01-20T17:29:32.938Z","avatar_url":"https://github.com/zcfan.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"@zcfan/p-task\n----\n\n```bash\nnpm i @zcfan/p-task\n```\n\nA tool for creating and managing the dependencies between asynchronous tasks.\n\nFor example, you have three tasks:\n- task1/2 has no dependencies, so after being created, they can run immediately after creation.\n- task3 depends on task1/2, so it'll run after task1/2 are `success`.\n\nYou can write them like this:\n\n```typescript\nconst task1 = new PTask(\n    [], \n    () =\u003e new Promise\u003cnumber\u003e(resolve =\u003e setTimeout(() =\u003e resolve(1), 500))\n)\nconst task2 = new PTask(\n    [], \n    () =\u003e new Promise\u003cstring\u003e(resolve =\u003e setTimeout(() =\u003e resolve('2'), 1000))\n)\nconst task3 = new PTask(\n    [task1, task2], \n    ([result1, result2]) =\u003e \n        new Promise\u003cnumber\u003e(resolve =\u003e setTimeout(() =\u003e resolve(result1 + Number(result2)), 500))\n)\n```\n\nTogether they form a bigger task(task3) which can help you treak all three tasks as a single task:\n\n- Get the final status:\n  - any task is `error`, the bigger task is `error`.\n  - if there is no `error`, any task is `pending`, the bigger task is `pending`.\n  - if all tasks are `success`, the bigger task is `success`.\n\n- Retry the bigger task:\n  - if the bigger task is `error`, you can retry it by calling `retry()`.\n  - it will only retry those `error` dependent tasks(or the bigger task itself, if it's `error`).\n  - those `success` or `pending` dependent tasks will not be retried.\n\n\u003e A real example: to implement a \"send messages that contain images\" feature in an instant chat system, there are two backend endpoints:\n\u003e - upload images that will be used in message.\n\u003e - send the message.\n\u003e \n\u003e Before sending a message, you need to upload all the images it uses first, and then send the message (images are represented by ids).\n\u003e \n\u003e If the image upload fails, or the image upload is successful but the message send fails, user are allowed to retry.\n\n----\n一个用于创建和管理异步任务之间的依赖关系的工具。\n\n比如有三个任务：\n- task1/2 没有依赖，所以在创建后可以立即运行。\n- task3 依赖于 task1/2，所以它会在 task1/2 成功后运行。\n\n你可以这样写：\n\n```typescript\nconst task1 = new PTask(\n    [], \n    () =\u003e new Promise\u003cnumber\u003e(resolve =\u003e setTimeout(() =\u003e resolve(1), 500))\n)\nconst task2 = new PTask(\n    [], \n    () =\u003e new Promise\u003cstring\u003e(resolve =\u003e setTimeout(() =\u003e resolve('2'), 1000))\n)\nconst task3 = new PTask(\n    [task1, task2], \n    ([result1, result2]) =\u003e \n        new Promise\u003cnumber\u003e(resolve =\u003e setTimeout(() =\u003e resolve(result1 + Number(result2)), 500))\n)\n```\n\n这三个任务组合起来，形成一个更大的任务(task3)，你可以把它们作为一个单独的任务来看待。\n\n- 获取大任务的整体状态：\n  - 任何任务是 `error`，则较大的任务是 `error`。\n  - 如果没有 `error`，任何任务是 `pending`，则较大的任务是 `pending`。\n  - 如果所有任务都是 `success`，则较大的任务是 `success`。\n\n- 重试大任务：\n  - 如果大任务是 `error`，你可以通过调用 `retry()` 来重试它。\n  - 它只会重试那些 `error` 的依赖任务（或者如果 task3 自己是 `error`，则会重试 task3）。\n  - 那些依赖于 `success` 或 `pending` 的任务不会被重试。\n\n\u003e 一个现实的例子：要实现一个即时聊天系统的发送图文消息功能，后台有两个接口：\n\u003e - 上传图片\n\u003e - 发送消息\n\u003e \n\u003e 在发送一条消息之前，需要先把它用到的图片上传完，然后再发送消息（其中图片用 id 表示）。\n\u003e \n\u003e 如果图片发送失败，或图片上传成功但发送消息失败，都需要允许用户重试。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcfan%2Fp-task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzcfan%2Fp-task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcfan%2Fp-task/lists"}