广度遍历深克隆
JAVASCRIPT- 1const getEmpty = (o) => {
 - 2  if (Array.isArray(o)) return [];
 - 3  if (o !== null && typeof o === "object") return {};
 - 4  return o;
 - 5};
 - 6
 - 7const cloneBfs = (a) => {
 - 8  const root = getEmpty(a),
 - 9    queue = [{ origin: a, copy: root }],
 - 10    map = new Map();
 - 11
 - 12  while (queue.length) {
 - 13    const { origin, copy } = queue.shift();
 - 14    for (let i in origin) {
 - 15      const empty = getEmpty(origin[i]);
 
限制 promise 并发数量
JAVASCRIPT- 1const pLimit = (limit) => {
 - 2  let count = 0;
 - 3  const task = [];
 - 4  return async (fn) => {
 - 5    if (count >= limit) {
 - 6      await new Promise((r) => task.push(r));
 - 7    }
 - 8    count++;
 - 9    try {
 - 10      const ret = await fn();
 - 11      return ret;
 - 12    } finally {
 - 13      count--;
 - 14      if (task.length) {
 - 15        task.shift()();
 
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 liangerwen's☻Blog !



