异步流程控制 遍历篇filter

news/2024/11/24 11:50:34/

文章目录

  • 基础方法
    • arrayEachIndexValue
    • baseEachIndexValue
    • symbolEachIndexValue
    • compact
    • timesSync
  • 异步遍历
    • filter
    • filterLimit
    • filterSeries

基础方法

arrayEachIndexValue

function arrayEachIndexValue(array, iterator, createCallback) {var value;var index = -1;var size = array.length;if (iterator.length === 3) {while (++index < size) {value = array[index];iterator(value, index, createCallback(index, value));}} else {while (++index < size) {value = array[index];iterator(value, createCallback(index, value));}}
}

baseEachIndexValue

function baseEachIndexValue(object, iterator, createCallback, keys) {var key, value;var index = -1;var size = keys.length;if (iterator.length === 3) {while (++index < size) {key = keys[index];value = object[key];iterator(value, key, createCallback(index, value));}} else {while (++index < size) {value = object[keys[index]];iterator(value, createCallback(index, value));}}
}

symbolEachIndexValue

function symbolEachIndexValue(collection, iterator, createCallback) {var value, item;var index = 0;var iter = collection[iteratorSymbol]();if (iterator.length === 3) {while ((item = iter.next()).done === false) {value = item.value;iterator(value, index, createCallback(index++, value));}} else {while ((item = iter.next()).done === false) {value = item.value;iterator(value, createCallback(index++, value));}}return index;
}

compact

  • 筛选数组中所有true的元素
function compact(array) {var index = -1;var size = array.length;var result = [];while (++index < size) {var value = array[index];if (value) {result[result.length] = value;}}return result;
}

timesSync

function timesSync(n, iterator) {var index = -1;while (++index < n) {iterator(index);}
}

异步遍历

filter

基本使用

// array
var order = [];
var array = [1, 3, 2];
var iterator = function(num, done) {setTimeout(function() {order.push(num);done(null, num % 2);}, num * 10);
};
async.filter(array, iterator, function(err, res) {console.log(res); // [1, 3];console.log(order); // [1, 2, 3]
});

实现

var filter = createFilter(arrayEachIndexValue, baseEachIndexValue, symbolEachIndexValue, true);function createFilter(arrayEach, baseEach, symbolEach, bool) {return function(collection, iterator, callback) {callback = callback || noop;var size, keys, result;var completed = 0;if (isArray(collection)) {size = collection.length;result = Array(size);arrayEach(collection, iterator, createCallback);} else if (!collection) {} else if (iteratorSymbol && collection[iteratorSymbol]) {result = [];size = symbolEach(collection, iterator, createCallback);size && size === completed && callback(null, compact(result));} else if (typeof collection === obj) {keys = nativeKeys(collection);size = keys.length;result = Array(size);baseEach(collection, iterator, createCallback, keys);}if (!size) {return callback(null, []);}function createCallback(index, value) { // 闭包记录索引和元素值return function done(err, res) {if (index === null) {throwError();}if (err) {index = null;callback = once(callback);callback(err);return;}if (!!res === bool) { // 异步回调结果为true时才记录result[index] = value; // 通过闭包index保存异步任务对应位置}index = null;if (++completed === size) { // 会等到所有任务执行完毕后,筛选出true的元素callback(null, compact(result));}};}};
}

filterLimit

  • 限制每次执行的异步任务数量
var filterLimit = createFilterLimit(true);function createFilterLimit(bool) {return function(collection, limit, iterator, callback) {callback = callback || noop;var size, index, key, value, keys, iter, item, iterate, result;var sync = false;var started = 0;var completed = 0;if (isArray(collection)) {size = collection.length;iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;} else if (!collection) {} else if (iteratorSymbol && collection[iteratorSymbol]) {size = Infinity;  // 注意迭代器类型不知道size有多少个result = [];iter = collection[iteratorSymbol]();iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;} else if (typeof collection === obj) {keys = nativeKeys(collection);size = keys.length;iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;}if (!size || isNaN(limit) || limit < 1) {return callback(null, []);}result = result || Array(size);timesSync(limit > size ? size : limit, iterate);function arrayIterator() {index = started++;if (index < size) {value = collection[index];iterator(value, createCallback(value, index));}}function arrayIteratorWithIndex() {index = started++;if (index < size) {value = collection[index];iterator(value, index, createCallback(value, index));}}function symbolIterator() {item = iter.next();if (item.done === false) {value = item.value;iterator(value, createCallback(value, started++));} else if (completed === started && iterator !== noop) { // completed === started 表明已迭代完毕,createCallback对于迭代器类型只负责++completediterator = noop;callback(null, compact(result));}}function symbolIteratorWithKey() {item = iter.next();if (item.done === false) {value = item.value;iterator(value, started, createCallback(value, started++));} else if (completed === started && iterator !== noop) {iterator = noop;callback(null, compact(result));}}function objectIterator() {index = started++;if (index < size) {value = collection[keys[index]];iterator(value, createCallback(value, index));}}function objectIteratorWithKey() {index = started++;if (index < size) {key = keys[index];value = collection[key];iterator(value, key, createCallback(value, index));}}function createCallback(value, index) {return function(err, res) {if (index === null) {throwError();}if (err) {index = null;iterate = noop;callback = once(callback);callback(err);return;}if (!!res === bool) {result[index] = value;}index = null;if (++completed === size) {callback = onlyOnce(callback);callback(null, compact(result));} else if (sync) {nextTick(iterate);} else {sync = true;iterate();}sync = false;};}};
}

filterSeries

  • 串行执行,在上一个任务done之后再执行下一个任务
var filterSeries = createFilterSeries(true);function createFilterSeries(bool) {return function(collection, iterator, callback) {callback = onlyOnce(callback || noop);var size, key, value, keys, iter, item, iterate;var sync = false;var completed = 0;var result = [];if (isArray(collection)) {size = collection.length;iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;} else if (!collection) {} else if (iteratorSymbol && collection[iteratorSymbol]) {size = Infinity;iter = collection[iteratorSymbol]();iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;} else if (typeof collection === obj) {keys = nativeKeys(collection);size = keys.length;iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;}if (!size) {return callback(null, []);}iterate();function arrayIterator() {value = collection[completed];iterator(value, done);}function arrayIteratorWithIndex() {value = collection[completed];iterator(value, completed, done);}function symbolIterator() {item = iter.next();value = item.value;item.done ? callback(null, result) : iterator(value, done);}function symbolIteratorWithKey() {item = iter.next();value = item.value;item.done ? callback(null, result) : iterator(value, completed, done);}function objectIterator() {key = keys[completed];value = collection[key];iterator(value, done);}function objectIteratorWithKey() {key = keys[completed];value = collection[key];iterator(value, key, done);}function done(err, res) {if (err) {callback(err);return;}if (!!res === bool) {result[result.length] = value;}if (++completed === size) {iterate = throwError;callback(null, result);} else if (sync) {nextTick(iterate);} else {sync = true;iterate();}sync = false;}};
}

http://www.ppmy.cn/news/36686.html

相关文章

常用的网络营销推广渠道方式

随着互联网的快速发展和普及&#xff0c;网络营销推广已成为企业和个人宣传和推广的主要方式之一。相比于传统的宣传方式&#xff0c;网络营销推广具有成本低、效果好、受众广等特点&#xff0c;因此备受欢迎。但是&#xff0c;网络营销推广的渠道和方式繁多&#xff0c;如何选…

MySQL高级第九篇:覆盖索引和索引条件下推等相关策略

MySQL高级第九篇&#xff1a;覆盖索引和索引条件下推等相关策略一、覆盖索引1. 什么是覆盖索引?2. 覆盖索引的好处避免lnnodb表进行索引的二次查询&#xff08;回表&#xff09;可以把随机 IO 变成顺序 IO 加快查询效率二、索引条件下推1. 举例&#xff1a;2. ICP的使用条件三…

Parasoft帮助中移智行顺利获得A-SPICE L3和ISO26262功能安全认证证书

2023年2月24日&#xff0c;国际独立第三方检测、检验和认证机构德国莱茵TV集团为中移智行网络科技有限公司&#xff08;以下简称“中移智行”&#xff09;颁布了A-SPICE L3和ISO26262功能安全产品ASIL B认证证书&#xff0c;标志着中移智行在软件质量体系管理和产品技术方面走在…

CE4003S2B1对循环流化床锅炉的调节

​CE4003S2B1对循环流化床锅炉的调节 循环流化床作为一种清洁高效燃烧技术在国际上被广泛认可&#xff0c;具有燃烧适应性广、燃烧效率高、氮氧化合物排量低、负荷调节范围大、污染物排放低等特点&#xff0c;属于环保型锅炉&#xff0c;是国家大力推广的新型锅炉。燃烧工艺如图…

rdma struct ibv_qp_attr属性timeout,retry_cnt,rnr_retry等字段含义。

如下&#xff1a; struct ibv_qp_attr {//...uint8_t timeout;uint8_t retry_cnt;uint8_t rnr_retry;//... };一&#xff1a;timeout字段 timeout表示等待ACK或NACK的无限时间。 这意味着如果消息中的任何包丢失&#xff0c;并且没有发送ACK或NACK&#xff0c;则不会发生…

VUE强制刷新渲染DOM

开始是 将获取到的数据给到 this.$api.coreStock.GetStockID({ id: data }).then((res) > { document.getElementById("mainconbarcode").value res.data.boxCode; }); 但后面影响数据保存 就想直接给到 dataForm.mainconbarcode res.data&#xff1b; this.…

C++ 98/03 应该学习哪些知识14

重载、重写和覆盖 C中的重载&#xff08;overloading&#xff09;、重写&#xff08;override&#xff09;和覆盖&#xff08;overriding&#xff09;是三个重要的概念&#xff0c;它们在C语言中都有着不同的意义和用法。在本文中&#xff0c;我们将详细解释这三个概念的含义&…

【基础算法】哈希表

系列综述&#xff1a; &#x1f49e;目的&#xff1a;本系列是个人整理为了秋招算法的&#xff0c;整理期间苛求每个知识点&#xff0c;平衡理解简易度与深入程度。 &#x1f970;来源&#xff1a;材料主要源于代码随想录进行的&#xff0c;每个算法代码参考leetcode高赞回答和…