[Javascript] Broadcaster + Operator + Listener pattern -- 8. Pass a Done Symbol when an Async Function is Done
JavaScriptasync -- is symbol an Function when
2023-09-14 08:59:13 时间
All good things come to an end. The concept of "done" plays a large part in our pattern and asynchronous code in general. We have to be able to handle when a broadcaster has completed its passing values so we can know what to do next.
import { curry } from "lodash"; const log = (v) => console.log(v); const done = Symbol("done"); export let createInterval = curry((time, listener) => { let i = 0; let id = setInterval(() => { listener(i++); }, time); return () => { clearInterval(id); }; }); const forOf = curry((itertor, listener) => { const id = setTimeout(() => { for (let item of itertor) { listener(item); } listener(done); }, 0); return () => { clearTimeout(id); }; }); let zip = curry((broadcaster1, broadcaster2, listener) => { let cancelBoth; let buffer1 = []; let cancel1 = broadcaster1((value) => { buffer1.push(value); if (buffer2.length) { listener([buffer1.shift(), buffer2.shift()]); if (buffer1[0] === done || buffer2[0] === done) { listener(done); cancelBoth(); } } }); let buffer2 = []; let cancel2 = broadcaster2((value) => { buffer2.push(value); if (buffer1.length) { listener([buffer1.shift(), buffer2.shift()]); if (buffer1[0] === done || buffer2[0] === done) { listener(done); cancelBoth(); } } }); cancelBoth = () => { cancel1(); cancel2(); }; return cancelBoth; }); let zipNumberIntervewl = zip(forOf([1, 2, 3]), createInterval(1000)); zipNumberIntervewl(log);
相关文章
- JavaScript实现复选框全选与全不选的效果
- Javascript Prototypes之旅(A Plain English Guide to JavaScript Prototypes译文)
- JavaScript之Window对象
- [Javascript] Safely Access a Property on a JavaScript Array with Optional Chaining
- [Javascript] Natively Format JavaScript Dates and Times
- [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
- [Javascript] Use JavaScript's for-in Loop on Objects with Prototypes
- [Javascript] Identify and Deal with NaN in JavaScript
- [Javascript] Filter out Duplicates from Flat JavaScript Array with array.filter / reduce / Set
- [Javascript] Natively Format JavaScript Dates and Times
- [Javascript] Understanding the difference between .prototype and .__proto__ in JavaScript
- [Javascript] Wait for the Fastest JavaScript Promise to Be Fulfilled with Promise.any()
- [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
- [Javascript] Create Your First Iterator in JavaScript
- [Javascript] Multiply Two Arrays over a Function in JavaScript
- [Javascript] Await a JavaScript Promise in an async Function with the await Operator
- [Javascript] Link to Other Objects through the JavaScript Prototype Chain
- [Javascript] JavaScript赋值时的传值与传址
- [Javascript] Delegate JavaScript (ES6) generator iteration control
- Unicode与JavaScript详解
- SAP CRM WebClient UI,点击Master Data工作中心后执行的JavaScript代码
- 如何比较两个对象以确定第一个对象包含与JavaScript中的第二个对象等效的属性值?
- 从零开始学_JavaScript_系列(19)——js系列<6>闭包
- 【 华为OD机试 2023】 上班之路/是否能到达公司(C++ Java JavaScript Python)
- 【 华为OD机试 2023】 最大连续文件之和 / 区块链文件转储系统(C++ Java JavaScript Python)
- 【华为OD机试 2023】 找数字、找等值元素(C++ Java JavaScript Python)
- springMVC项目异步错误处理请求Async support must be enabled on a servlet and for all filters involved in async
- JavaScript-html标题滚动效果
- 理解javascript中的with关键字
- JavaScript 各种事件触发总结
- JavaScript脚本引擎SpiderMonkey