{"id":13607667,"url":"https://github.com/dvlsg/async-csp","last_synced_at":"2026-01-08T17:06:17.975Z","repository":{"id":33811290,"uuid":"37506855","full_name":"dvlsg/async-csp","owner":"dvlsg","description":"CSP style channels using ES7 async/await","archived":false,"fork":false,"pushed_at":"2017-11-08T03:45:46.000Z","size":615,"stargazers_count":318,"open_issues_count":6,"forks_count":18,"subscribers_count":9,"default_branch":"develop","last_synced_at":"2025-10-19T16:51:25.285Z","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/dvlsg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2015-06-16T03:56:05.000Z","updated_at":"2025-06-04T15:14:02.000Z","dependencies_parsed_at":"2022-07-28T20:59:50.151Z","dependency_job_id":null,"html_url":"https://github.com/dvlsg/async-csp","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/dvlsg/async-csp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlsg%2Fasync-csp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlsg%2Fasync-csp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlsg%2Fasync-csp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlsg%2Fasync-csp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dvlsg","download_url":"https://codeload.github.com/dvlsg/async-csp/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlsg%2Fasync-csp/sbom","scorecard":{"id":360899,"data":{"date":"2025-08-11","repo":{"name":"github.com/dvlsg/async-csp","commit":"98807016d110f0eb3d2d9ba5d6e2d5b5b5b19266"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.7,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 2/26 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'develop'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 6 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"65 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-8w4h-3cm3-2pm2","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-832h-xg76-4gv6","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx","Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-xg47-r67p-vhv5","Warn: Project is vulnerable to: GHSA-xrmp-99wj-p6jc","Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-hr2v-3952-633q","Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9","Warn: Project is vulnerable to: GHSA-qrmc-fj45-qfc2","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3","Warn: Project is vulnerable to: MAL-2023-462","Warn: Project is vulnerable to: GHSA-xf7w-r453-m56c","Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5","Warn: Project is vulnerable to: GHSA-44pw-h2cw-w3vq","Warn: Project is vulnerable to: GHSA-jp4x-w63m-7wgm","Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp","Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj","Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37","Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546","Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx","Warn: Project is vulnerable to: GHSA-896r-f27r-55mw","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq","Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695","Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm","Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574","Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm","Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9","Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm","Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw","Warn: Project is vulnerable to: GHSA-2m96-9w4j-wgv7","Warn: Project is vulnerable to: GHSA-h726-x36v-rx45","Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-hxm2-r34f-qmc5","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-6g33-f262-xjp4","Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-2m39-62fm-q8r3","Warn: Project is vulnerable to: GHSA-mf6x-7mm4-x2g7","Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m","Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9","Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh","Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36","Warn: Project is vulnerable to: GHSA-52f5-9888-hmc6","Warn: Project is vulnerable to: GHSA-g7q5-pjjr-gqvp","Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3","Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh","Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-18T10:49:41.138Z","repository_id":33811290,"created_at":"2025-08-18T10:49:41.138Z","updated_at":"2025-08-18T10:49:41.138Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28246998,"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":"2026-01-08T02:00:06.591Z","response_time":241,"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-08-01T19:01:20.546Z","updated_at":"2026-01-08T17:06:17.947Z","avatar_url":"https://github.com/dvlsg.png","language":"JavaScript","readme":"# async-csp\n[Communicating sequential processes](https://wikipedia.org/wiki/Communicating_sequential_processes) for use with ES2016's async/await syntax.\n\nHere's [GoLang's ping/pong example](https://talks.golang.org/2013/advconc.slide#6) in `async-csp` flavor:\n\n```js\nimport Channel from 'async-csp'\n\nasync function sleep(duration) {\n    return new Promise(resolve =\u003e setTimeout(resolve, duration))\n}\n\nasync function player(name, table) {\n    while (true) {\n        let ball = await table.take();\n        if (ball === Channel.DONE) {\n            console.log(`${name}: table's gone!`);\n            break;\n        }\n        ball.hits++;\n        console.log(`${name}! Hits: ${ball.hits}`);\n        await sleep(100);\n        await table.put(ball);\n    }\n}\n\nasync function pingPong() {\n    console.log('Opening ping-pong channel!');\n    let table = new Channel();\n\n    player('ping', table);\n    player('pong', table);\n\n    console.log('Serving ball...');\n    let ball = {hits: 0};\n    await table.put(ball);\n    await sleep(1000);\n\n    console.log('Closing ping-pong channel...');\n    table.close();\n\n    await table.done();\n    console.log('Channel is fully closed!');\n    console.log(`Ball was hit ${ball.hits} times!`);\n}\n\npingPong()\n```\n\nSometimes the output of this example is\n\n```\nOpening ping-pong channel!\nServing ball...\nping! Hits: 1\npong! Hits: 2\nping! Hits: 3\npong! Hits: 4\nping! Hits: 5\npong! Hits: 6\nping! Hits: 7\npong! Hits: 8\nping! Hits: 9\nClosing ping-pong channel...\npong: table's gone!\nChannel is fully closed!\nBall was hit 9 times!\nping: table's gone!\n```\n\nand sometimes it's\n\n```\nOpening ping-pong channel!\nServing ball...\nping! Hits: 1\npong! Hits: 2\nping! Hits: 3\npong! Hits: 4\nping! Hits: 5\npong! Hits: 6\nping! Hits: 7\npong! Hits: 8\nping! Hits: 9\npong! Hits: 10\nClosing ping-pong channel...\nping: table's gone!\nChannel is fully closed!\nBall was hit 10 times!\npong: table's gone!\n```\n\nSometimes the ball is hit 9 times, and sometimes 10! This is due to the nature of asynchronicity which is nicely depicted in this example.\n\n## Installation\n\n```\nnpm install async-csp\n```\n\n## Default Task\n\n* Install node.js\n* Clone the async-csp project\n* Run `npm install`\n* Run `gulp`\n    * Executes tests\n    * Cleans dist\n    * Lints source\n    * Builds source\n    * Watches source and tests\n\n## Examples\n\nExamples can be found [here](examples/).\nTo run any example, make sure the default task has been successfully run once (or at least `npm install`), then run `node index.js` from the root folder of the example.\n\n## Usage\n\n*Note: All of the code pieces below are assumed to be executed from an `async` context, so `await` is available at the base level. To read more about these methods, see [this proposal](https://github.com/lukehoban/ecmascript-asyncawait) for async/await in ES7.*\n\n### Data Flow\n\nA `Channel` is a container which makes use of `Promises` to handle the incoming and outgoing flow of data.\n\nTo put a value on a `Channel` use `Channel#put()`, and to take a value from the channel use `Channel#take()`.\n\nBy default, the promise returned from `Channel#put()` will not resolve until its value is taken from the channel, and the promise returned from `Channel#take()` will not resolve until a value can be taken from the channel.\n\n```js\nimport Channel from 'async-csp';\n\nlet channel = new Channel();\n\nasync function puts(ch) {\n    await ch.put(1); // resolves when the first ch.take() is executed\n    await ch.put(2); // resolves when the second ch.take() is executed\n    await ch.put(3); // resolves when the third ch.take() is executed\n}\n\nasync function takes(ch) {\n    console.log(await ch.take()); // resolves to 1, from the first ch.put()\n    console.log(await ch.take()); // resolves to 2, from the second ch.put()\n    console.log(await ch.take()); // resolves to 3, from the third ch.put()\n}\n\nputs(channel);\ntakes(channel);\n```\n\n### Buffering\n\nA `Channel` can be created with a buffer for receiving puts.\nEssentially, this means a put can resolve while space is available on the buffer, even if no take is waiting to receive a value.\nAs soon as the buffer becomes full, put will begin blocking again until a take clears a space from the buffer.\n\nTo create a `Channel` with a buffer, pass in a `Number` as the first argument to the constructor.\n\n```js\nimport Channel, { timeout } from 'async-csp';\n\nlet channel = new Channel(2); // buffer size of 2\n\nasync function puts(ch) {\n    await ch.put(1); //=\u003e this can resolve immediately, taking one space on the buffer\n    console.log('after put 1'); // fires immediately\n    await ch.put(2); //=\u003e this can also resolve immediately, taking the second space on the buffer\n    console.log('after put 2'); // also fires immediately\n    await ch.put(3); //=\u003e buffer is full! this will block until another process takes a value from the Channel\n    console.log('after put 3'); // fires after the unblock!\n}\n\nasync function takes(ch) {\n    console.log(await ch.take()); //=\u003e resolves to 1, clears a space on the buffer and allows the blocked ch.put(3) to also resolve\n    console.log(await ch.take()); //=\u003e resolves to 2\n    console.log(await ch.take()); //=\u003e resolves to 3\n}\n\n// execute the puts right away\nputs(channel);\n\n// use a helper method to wait for 1 second\n// to help show the effects of blocking\nawait timeout(1000);\n\n// after 1 second, start executing takes\ntakes(channel);\n```\n\n### Non-blocking puts\n\nA common use for a `Channel` requires data to be input from a non async context, or without waiting for the put to resolve.\n\nIn this scenario, do not await the result of `Channel#put()`.\n\n```js\nlet ch = new Channel();\n\n// non-blocking puts, don't use `await`\nch.put(1);\nch.put(2);\nch.put(3);\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\n```\n\n### Transforming\n\nWhen constructing a `Channel`, you can pass in a callback to transform values as they are taken.\n\n```js\nlet ch = new Channel(x =\u003e x * 2);\n\nch.put(1);\nch.put(2);\nch.put(3);\n\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 4\nconsole.log(await ch.take()); //=\u003e 6\n```\n\nIf values should be dropped from the `Channel`, simply return `undefined` from the transform callback.\n\n```js\nlet ch = new Channel(x =\u003e {\n    if (x \u003e 2)\n        return x;\n});\n\nch.put(1);\nch.put(1);\nch.put(3);\nch.put(4);\n\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\n```\n\nIf a transform needs to expand a single value into multiple values, use the `push` parameter with the transform.\n\nNote that when using this callback style, all values must be sent through `push`.\nAny value returned from the transform callback will be ignored\nwhen the provided transformer has more than one parameter defined.\n\n```js\nlet ch = new Channel((x, push) =\u003e {\n    push(x);\n    push(x + 1);\n});\n\nch.put(1);\nch.put(3);\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\n```\n\nIf the transform needs to work asynchronously, there are a few ways to accomplish this.\n\nThe first is to use an async callback.\n\n```js\nlet ch = new Channel(async x =\u003e {\n    await timeout(100);\n    return x;\n});\n\nch.put(1);\nch.put(2);\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\n```\n\nThe second way to use an asynchronous transform is by passing in an async callback with a parameter length of 2.\nSimilar to the non-async callback with a parameter length of 2, all values must be sent through `push`,\nand returned values will be ignored.\n\n```js\nlet ch = new Channel(async(x, push) =\u003e {\n    await timeout(100);\n    push(x);\n    await timeout(100);\n    push(x + 1);\n});\n\nch.put(1);\nch.put(3);\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\n```\n\nThe final way to use an asynchronous transform is with a three-parameter callback.\nTo signify that the transform has completed, execute the third argument.\n\n```js\nlet ch = new Channel((x, push, done) =\u003e {\n    push(x);\n    setTimeout(() =\u003e {\n        push(x + 1);\n        done();\n    }, 100);\n});\n\nch.put(1);\nch.put(3);\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\n```\n\nOne final note: Using a transform does not prevent you from simultaneously using a buffer.\nTo use a transform with a buffered `Channel`, pass in the buffer size as the first argument, and the transform as the second.\n\n```js\nlet ch = new Channel(2, x =\u003e x + 1);\n\n// note that puts will be resolved immediately, since we have space on the buffer\nawait ch.put(1);\nawait ch.put(3);\n\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 4\n```\n\n### Channel#pipe()\n\nSimilarly to `Streams`, `Channels` can be piped from one to another.\n\n```js\nlet ch1 = new Channel();\nlet ch2 = new Channel();\nlet ch3 = new Channel();\n\nch1.pipe(ch2).pipe(ch3);\n/*\n    +---+\n    |ch1|\n    +---+\n      |\n      V\n    +---+\n    |ch2|\n    +---+\n      |\n      V\n    +---+\n    |ch3|\n    +---+\n*/\n\nch1.put(1);\nch1.put(2);\nch1.put(3);\n\nconsole.log(await ch3.take()); //=\u003e  1\nconsole.log(await ch3.take()); //=\u003e  2\nconsole.log(await ch3.take()); //=\u003e  3\n```\n\nA `Channel` can be piped to multiple destinations.\nIn this case, downstream `Channels` will receive every value from upstream.\n\n```js\nlet ch1 = new Channel();\nlet ch2 = new Channel();\nlet ch3 = new Channel();\n\nch1.pipe(ch2, ch3); // or `ch1.pipe(ch2); ch1.pipe(ch3);`\n/*\n        +---+\n      +-|ch1|-+\n      | +---+ |\n      |       |\n      V       V\n    +---+   +---+\n    |ch2|   |ch3|\n    +---+   +---+\n*/\n\nch1.put(1);\nch1.put(2);\nch1.put(3);\n\n// 1 is taken from ch1 and put on ch2 and ch2\n\nconsole.log(await ch2.take()); //=\u003e  1\nconsole.log(await ch3.take()); //=\u003e  1\n\n// 2 is taken from ch1 and put on ch2 and ch3\n\nconsole.log(await ch2.take()); //=\u003e  2\nconsole.log(await ch3.take()); //=\u003e  2\n\n// 3 is taken from ch1 and put on ch2 and ch3\n\nconsole.log(await ch2.take()); //=\u003e  3\nconsole.log(await ch3.take()); //=\u003e  3\n```\n\nTypically, it is recommended to pass a `Channel` into pipe, but if you need a shortcut for creating a `Channel` from a transform callback, you may also pass in a callback directly.\n\n```js\nlet ch1 = new Channel(x =\u003e x + 1);\n\n// note that this returns a reference to the last `Channel` from `#pipe()`\nlet ch3 = ch1.pipe(x =\u003e x + 2).pipe(x =\u003e x + 3); \n\nch1.put(1);\nch1.put(2);\nch1.put(3);\n\nconsole.log(await ch3.take()); //=\u003e 7\nconsole.log(await ch3.take()); //=\u003e 8\nconsole.log(await ch3.take()); //=\u003e 9\n```\n\nAlso take note that if one downstream `Channel` is blocked from a currently unresolved `Channel#put()` (buffered or non-buffered), then the *entire* pipe will be blocked.\nIn the example above, an attempt to take all 3 values from `ch2` before taking any values from `ch3` would have resulted in deadlock.\n\nFinally, any piped `Channel` will also execute transforms.\n\n```js\nlet ch1 = new Channel(x =\u003e x + 2);\nlet ch2 = new Channel(x =\u003e x.toString());\nlet ch3 = new Channel(x =\u003e ({ x: x }));\n\nch1.pipe(ch2).pipe(ch3);\n\nch1.put(1);\nch1.put(2);\nch1.put(3);\n\nconsole.log(await ch3.take()); //=\u003e { x: '3' }\nconsole.log(await ch3.take()); //=\u003e { x: '4' }\nconsole.log(await ch3.take()); //=\u003e { x: '5' }\n```\n\n### Channel.pipeline()\n\n`Channel.pipeline()` is a helper method for creating piped channels from any number of callbacks or `Channels`.\nInputs can be provided either as separate arguments, or contained in an array as the first argument. If an input is provided as a callback, it will be turned into a `Channel` using that callback as the transform.\n\n`Channel.pipeline()` will return an array containing the first and the last `Channel` in the pipeline.\n\n```js\nlet [ ch1, ch3 ] = Channel.pipeline(\n    x =\u003e x + 2,\n    new Channel(x =\u003e x.toString()),\n    async x =\u003e ({ x })\n);\n\nch1.put(1);\nch1.put(2);\nch1.put(3);\n\nconsole.log(await ch3.take()); //=\u003e { x: '3' }\nconsole.log(await ch3.take()); //=\u003e { x: '4' }\nconsole.log(await ch3.take()); //=\u003e { x: '5' }\n```\n\n### Channel#unpipe()\n\nIf a `Channel` should be taken out of an existing pipe, use `Channel#unpipe()`.\n\n```js\nlet ch1 = new Channel();\nlet ch2 = new Channel();\nlet ch3 = new Channel();\n\nch1.pipe(ch2).pipe(ch3);\n\nch1.put(1);\nconsole.log(await ch3.take()); //=\u003e 1\n\n// now take ch2 out of the pipe\nch1.unpipe(ch2);\n\nch1.put(2);\nconsole.log(await ch1.take()); //=\u003e 2, note that we took from ch1\n\n// note that ch2 is still piping to ch3\nch2.put(3);\nconsole.log(await ch3.take()); //=\u003e 3\n```\n\n### Channel#merge()\n\n`Channel.merge()` is a helper method for piping multiple `Channels` into a single, new `Channel`.\n\n```js\nlet ch1 = new Channel();\nlet ch2 = new Channel();\nlet ch3 = ch1.merge(ch2); // or, `ch3 = Channel.merge(ch1, ch2)`\n\nch1.put(1);\nch2.put(2);\n\nconsole.log(await ch3.take()); //=\u003e 1\nconsole.log(await ch3.take()); //=\u003e 2\n```\n\n### Channel#close()\n\nA `Channel` has 3 states: open, closed, and ended. An open `Channel` can be written to, a closed `Channel` will not accept any new values but may be non-empty, and an ended `Channel` is both closed and empty.\n\nTo signify that a `Channel` should be done accepting new values, execute `Channel#close()`. Data can still be taken from the channel after that point, but no more values can be added.\n\n```js\nlet ch1 = new Channel();\n\nch1.put(1);\nch1.put(2);\n\nch1.close();\n\nch1.put(3); // resolves immediately with value of Channel.DONE\n\nconsole.log(await ch1.take()); //=\u003e 1\nconsole.log(await ch1.take()); //=\u003e 2\nconsole.log(await ch1.take()); //=\u003e Channel.DONE\n```\n\nIf `Channels` are piped together, and you want the entire pipeline to close when possible, simply pass `true` as an argument to `Channel#close()`.\n\n```js\nlet ch1 = new Channel();\nlet ch2 = new Channel();\nch1.pipe(ch2);\n\nch1.put(1);\nch1.put(2);\n\nch1.close(true);\n\nconsole.log(await ch2.take()); //=\u003e 1\nconsole.log(await ch2.take()); //=\u003e 2\nconsole.log(await ch2.take()); //=\u003e Channel.DONE\n```\n\n### Channel#done()\n\nIn order to wait for a channel to be ended (closed and empty), await the resolution of `done`.\n\n```js\nlet ch = new Channel();\n\nch.put(1);\nch.put(2);\nch.close();\n\nlet arr = [];\n(async() =\u003e {\n    await timeout(1000);\n    arr.push(await ch.take());\n    await timeout(1000);\n    arr.push(await ch.take());\n})();\n\nawait ch.done(); // will not resolve until the async IIFE takes both values from the channel\nconsole.log(arr); //=\u003e [ 1, 2 ]\n```\n\n### Channel#tail()\n\nWhile manually appending values to a `Channel` can be accomplished,\nit often becomes significantly more difficult\nwhen items such as pipes and asynchronous transforms are in play.\n\nFor simplicity, `Channel#tail()` is provided as an alternative method for\nproviding values to `Channel#take()` only after the `Channel` is closed\nand all existing `Channel#put()`s have been resolved.\n\n```js\nlet ch = new Channel();\n\nch.put(1);\nch.tail(4);\nch.put(2);\nch.put(3);\nch.close();\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\n```\n\nNote that when a `Channel` has a transform, any values provided through `Channel#tail()`\nwill also use that transform.\n\n```js\nlet ch = new Channel(x =\u003e x + 2);\n\nch.put(1);\nch.tail(4);\nch.put(2);\nch.put(3);\nch.close();\n\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\nconsole.log(await ch.take()); //=\u003e 5\nconsole.log(await ch.take()); //=\u003e 6\n```\n\n### Channel#consume()\n\nIf you would like to execute a callback as soon as values can be taken from the `Channel`,\nyou may add a consumer by using `Channel#consume()`.\n\n```js\nlet ch = new Channel();\nch.consume(x =\u003e {\n    console.log(x);\n});\n\nawait ch.put(1);\nawait ch.put(2);\nawait ch.put(3);\nawait ch.put(4);\n\n// console logs\n//=\u003e 1\n//=\u003e 2\n//=\u003e 3\n//=\u003e 4\n```\n\n`Channel#consume()` can also be handled asynchronously, and will not attempt to queue up another `Channel#take()`\nuntil the consumer callback has completed running.\n\n```js\nlet ch = new Channel();\nlet arr = [];\nch.consume(async x =\u003e {\n    await timeout(1000);\n    arr.push(x);\n    console.log(x);\n});\n\nawait ch.put(1);\nawait ch.put(2);\nawait ch.put(3);\nawait ch.put(4);\n\n// console logs, once a second\n//=\u003e 1\n//=\u003e 2\n//=\u003e 3\n//=\u003e 4\n```\n\n### Channel#produce()\n\nSimilar to `Channel#consume()`, `Channel#produce()` will put returned values\nonto the `Channel` as soon as space becomes available.\n\n```js\nlet ch = new Channel();\nlet counter = 0;\nch.produce(() =\u003e ++counter);\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\n```\n\nAs with `Channel#consume()`, `Channel#produce()` can also work asynchronously.\n\n```js\nlet ch = new Channel();\nlet counter = 0;\nch.produce(async() =\u003e {\n    await timeout(1000);\n    return ++counter;\n});\n\nconsole.log(await ch.take()); //=\u003e 1, after 1 second\nconsole.log(await ch.take()); //=\u003e 2, after 2 seconds\nconsole.log(await ch.take()); //=\u003e 3, after 3 seconds\nconsole.log(await ch.take()); //=\u003e 4, after 4 seconds\n```\n\n### Channel.from()\n\nIf you have an iterable item which you would like to convert into a `Channel`,\nuse `Channel.from()` to construct a `Channel` from that iterable.\n\n```js\nlet arr = [ 1, 2, 3 ];\nlet ch = Channel.from(arr);\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\n```\n\nNote that in this case, a buffer is created with the size of the iterable,\nall values are placed directly onto the buffer, and the `Channel` is marked as closed,\nwhich will include any attached downstream pipes.\n\nIf the channel or any downstream pipes should remain open to continue receiving puts,\npass in a `true` as the second argument.\n\n```js\nlet arr = [ 1, 2, 3 ];\nlet ch = Channel.from(arr, true);\nch.put(4);\nch.close();\n\nconsole.log(await ch.take()); //=\u003e 1\nconsole.log(await ch.take()); //=\u003e 2\nconsole.log(await ch.take()); //=\u003e 3\nconsole.log(await ch.take()); //=\u003e 4\n```\n\n## License\n\nAll code released under the [MIT](https://github.com/dvlsg/async-csp/blob/master/LICENSE) license.\n","funding_links":[],"categories":["JavaScript","Libraries"],"sub_categories":["[Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvlsg%2Fasync-csp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdvlsg%2Fasync-csp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvlsg%2Fasync-csp/lists"}