{"id":20461386,"url":"https://github.com/antvis/async-hook","last_synced_at":"2025-04-13T06:17:36.472Z","repository":{"id":48982493,"uuid":"243275396","full_name":"antvis/async-hook","owner":"antvis","description":"the control flow for l7","archived":false,"fork":false,"pushed_at":"2022-10-24T07:27:56.000Z","size":32,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":21,"default_branch":"master","last_synced_at":"2024-10-29T21:05:30.214Z","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/antvis.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}},"created_at":"2020-02-26T13:56:53.000Z","updated_at":"2022-11-07T09:14:50.000Z","dependencies_parsed_at":"2023-01-20T09:06:53.889Z","dependency_job_id":null,"html_url":"https://github.com/antvis/async-hook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antvis%2Fasync-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antvis%2Fasync-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antvis%2Fasync-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antvis%2Fasync-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antvis","download_url":"https://codeload.github.com/antvis/async-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246719088,"owners_count":20822673,"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-11-15T12:25:08.930Z","updated_at":"2025-04-13T06:17:36.451Z","avatar_url":"https://github.com/antvis.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `async-hook`\n\n\u003e L7 数据流管理工具\n\n## Usage\n\n```js\nconst asyncHook = require('async-hook');\n\n```\n## hooks\n\n`async-hook` 提供了一系列的 `hook` 方法用于控制方法的调用\n### SyncHook\nSyncHook 支持对一组方法的顺序调用，同时 `call` 方法传入的参数会传递个方法组的每个方法使用\n```js\nconst hook = new SyncHook();\nconst funcList = [\n    (args) =\u003e {\n        console.log(`step1 - ${args}`);\n    },\n    (args) =\u003e{\n        console.log(`step2 - ${args}`);\n    },\n    (args) =\u003e{\n        console.log(`step3 - ${args}`);\n    },\n];\nhook.tap('test', funcList);\nhook.call('call');\n// step1 - call, step2 - call, step3 - call\n```\n\n### SyncBailHook\n使用方法类似 SyncHook，但是不支持 `call` 传值\n\n### SyncWaterfallHook\nSyncWaterfallHook 支持对一组方法的顺序调用，正常情况下方法组会在执行 `call` 方法之后全部执行，但可以通过每个函数的返回值控制提前结束\n```js\nconst hook = new SyncHook();\nconst funcList = [\n    () =\u003e { // 执行第一个函数\n        console.log('step1');\n        return true;\n    },\n    () =\u003e{ // 上一个函数返回值为 true，继续往下执行\n        // do something\n        console.log('step2');\n        return false\n    },\n    () =\u003e{ // 上一个函数返回值为 false（void、null、undefined、0），不再往下执行\n        // do something\n        console.log('step3');\n        return true\n    },\n];\nhook.tap('test', funcList);\nhook.call('call');\n// step1, step2\n```\n### AsyncSeriesHook\n使用类似 SyncHook，不过方法组中都是异步方法\n\n### AsyncSeriesBailHook\n使用类似 AsyncSeriesHook，但是不支持 `call` 传值\n\n### AsyncWaterfallHook\nAsyncWaterfallHook 支持对一组异步方法的顺序调用，正常情况下方法组会在执行 `call` 方法之后全部执行，但可以通过每个函数的返回值控制提前结束\n```js\nconst hook = new AsyncWaterfallHook();\nconst funcList = [\n    () =\u003e { // 执行第一个函数\n        console.log('step1');\n        return new Promise((resolve) =\u003e setTimeout(() =\u003e resolve(true), 100))\n    },\n    async () =\u003e{ // 上一个函数返回值为 true，继续往下执行\n        // do something\n        console.log('step2');\n        return true\n    },\n    async () =\u003e{ // 上一个函数返回值为 true，继续往下执行\n        // do something\n        console.log('step3');\n        return false\n    },\n    async () =\u003e{ // 上一个函数返回值为 false（void、null、undefined、0），不再往下执行\n        // do something\n        console.log('step4');\n        return true\n    },\n];\nhook.tapPromise('test', funcList);\nhook.promise();\n// step1, step2, step3\n```\n### AsyncParallelHook\n控制一组异步方法的并行调用，类似 `Promise.all`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantvis%2Fasync-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantvis%2Fasync-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantvis%2Fasync-hook/lists"}