{"id":18749110,"url":"https://github.com/lefex/sy-task","last_synced_at":"2025-11-25T23:30:21.631Z","repository":{"id":85249908,"uuid":"361794314","full_name":"lefex/sy-task","owner":"lefex","description":"Easier to perform sync or async tasks","archived":false,"fork":false,"pushed_at":"2021-05-07T00:19:13.000Z","size":129,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-20T10:11:22.260Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lefex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2021-04-26T15:10:14.000Z","updated_at":"2021-05-08T05:48:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"4e56bff2-7b95-4018-8840-bd220d3e8f9e","html_url":"https://github.com/lefex/sy-task","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/lefex/sy-task","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefex%2Fsy-task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefex%2Fsy-task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefex%2Fsy-task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefex%2Fsy-task/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lefex","download_url":"https://codeload.github.com/lefex/sy-task/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefex%2Fsy-task/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286079811,"owners_count":27282121,"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-11-25T02:00:05.816Z","response_time":54,"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":"2024-11-07T17:06:25.797Z","updated_at":"2025-11-25T23:30:21.625Z","avatar_url":"https://github.com/lefex.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sy-task\n\neasy run sync or asyn task\n\n## install\n\n```shell\nnpm install sy-task\n```\n\n## run this project\n\n```shell\nnpm run dev\n\nthen:\nopen in browser http://localhost:8888/pipeline.html\nopen in browser http://localhost:8888/queue.html\n```\n\n## pipeline\n\nAdd three tasks to pipeline, they will run one by one. You can call next function to run next task that in pipeline.\n\n```js\n// create a pipeline\n// The data can be used in every task\nlet data = {};\nlet pipeline = new SYPipeline(data);\n\n// The use method to add task to pipeline\n// add sync task to pipeline, the task must a function that have (data, next) params\npipeline.use((data, next) =\u003e {\n    doSomething();\n    // run next task\n    next();\n});\n\n// add async task to pipeline\npipeline.use((data, next) =\u003e {\n    setTimeout(() =\u003e {\n        // you can change data, add property taskId to data\n        data.taskId = 'everydata';\n        next();\n    }, 200);\n});\n\n// add sync task to pipeline\npipeline.use(function (data, next) {\n    if (data.taskId) {\n        // find the task, stop run\n    }\n    else {\n        next();\n    }\n});\n\n// the task begin run\npipeline.run();\n```\n\n## queue.race\n\nRun task one by one, if the task resolve a truely value, the queue stop.\n\n```js\n// a task\nconst everyDay = (data) =\u003e {\n    return false;\n};\n\nconst newUser = (data) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        setTimeout(() =\u003e {\n            resolve('new user');\n        }, 100);\n    });\n};\n\nconst vip = (data) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        setTimeout(() =\u003e {\n            resolve('vip');\n        }, 10);\n    });\n};\n\n// create a SYQueue\nlet queue = new SYQueue();\n// crate a task array\nlet tasks = [everyDay, newUser, vip];\n// find the first resolve task\nqueue.race(tasks).then(res =\u003e {\n    // the result is new user\n    console.log('queue finished =', res);\n}).catch(error =\u003e {\n    console.log('error = ', error);\n});\n```\n\n## queue.serial\n\nRun task one by one until all task finish.\n\n```js\n// a task\nconst everyDay = (data) =\u003e {\n    return false;\n};\n\nconst newUser = (data) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        setTimeout(() =\u003e {\n            resolve('new user');\n        }, 100);\n    });\n};\n\nconst vip = (data) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        setTimeout(() =\u003e {\n            resolve('vip');\n        }, 10);\n    });\n};\n\n// create a SYQueue\nlet queue = new SYQueue();\n// crate a task array\nlet tasks = [everyDay, newUser, vip];\n// run task one by one\nqueue.serial(tasks).then(res =\u003e {\n    // res {\n    //     0: false,\n    //     1: new user,\n    //     2: vip\n    // }\n    console.log('queue finished =', res);\n}).catch(error =\u003e {\n    console.log('error = ', error);\n});\n```\n\n## queue.parallel\n\nRun task parallelly, you can set the value to limit how many task to run. One task will begin run when a task resolve. SYQueue will stop when all tasks finish.\n\n```js\n// a task\nconst everyDay = (data) =\u003e {\n    return false;\n};\n\nconst newUser = (data) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        setTimeout(() =\u003e {\n            resolve('new user');\n        }, 100);\n    });\n};\n\nconst vip = (data) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        setTimeout(() =\u003e {\n            resolve('vip');\n        }, 10);\n    });\n};\n\n// create a SYQueue\nlet queue = new SYQueue();\n// crate a task array\nlet tasks = [everyDay, newUser, vip];\n// only two tasks can run at a time\nqueue.parallel(tasks, 2).then(res =\u003e {\n    // res {\n    //     0: false,\n    //     1: new user,\n    //     2: vip\n    // }\n    console.log('queue finished =', res);\n}).catch(error =\u003e {\n    console.log('error = ', error);\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flefex%2Fsy-task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flefex%2Fsy-task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flefex%2Fsy-task/lists"}