收藏
回答

构建npm包时报错(SyntaxError: Unexpected token )?

npm install p-limit安装好后,在构建npm时报错SyntaxError: Unexpected token (28:10)

p-limit: index.js

import Queue from 'yocto-queue';
export default function pLimit (concurrency) {
  if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
    throw new TypeError('Expected `concurrency` to be a number from 1 and up');
  }
  const queue = new Queue();
  let activeCount = 0;
  const next = () => {
    activeCount--;
    if (queue.size > 0) {
      queue.dequeue()();
    }
  };
  const run = async (fn, resolve, args) => {
    activeCount++;
    const result = (async () => fn(...args))();
    resolve(result);
    try {
      await result;
    } catch { }
    next();
  };
  const enqueue = (fn, resolve, args) => {
    queue.enqueue(run.bind(undefined, fn, resolve, args));
    (async () => {
      // This function needs to wait until the next microtask before comparing
      // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
      // when the run function is dequeued and called. The comparison in the if-statement
      // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
      await Promise.resolve();
      if (activeCount < concurrency && queue.size > 0) {
        queue.dequeue()();
      }
    })();
  };
  const generator = (fn, ...args) => new Promise(resolve => {
    enqueue(fn, resolve, args);
  });
  Object.defineProperties(generator, {
    activeCount: {
      get: () => activeCount,
    },
    pendingCount: {
      get: () => queue.size,
    },
    clearQueue: {
      value: () => {
        queue.clear();
      },
    },
  });
  return generator;
}


yocto-queue: index.js

/*
How it works:
`this.#head` is an instance of `Node` which keeps track of its current value and nests another 
instance of `Node` that keeps the value that comes after it. When a value is provided to `.enqueue()`, 
the code needs to iterate through `this.#head`, going deeper and deeper to find the last value. However, 
iterating through every single item is slow. This problem is solved by saving a reference to the last 
value as `this.#tail` so that it can reference it to add a new value.
*/

class Node {
  value;
  next;
  constructor(value) {
    this.value = value;
  }
}
export default class Queue {
  #head;
  #tail;
  #size;
  constructor() {
    this.clear();
  }
  enqueue(value) {
    const node = new Node(value);
    if (this.#head) {
      this.#tail.next = node;
      this.#tail = node;
    } else {
      this.#head = node;
      this.#tail = node;
    }
    this.#size++;
  }
  dequeue() {
    const current = this.#head;
    if (!current) {
      return;
    }
    this.#head = this.#head.next;
    this.#size--;
    return current.value;
  }
  clear() {
    this.#head = undefined;
    this.#tail = undefined;
    this.#size = 0;
  }
  get size() {
    return this.#size;
  }
  * [Symbol.iterator]() {
    let current = this.#head;
    while (current) {
      yield current.value;
      current = current.next;
    }
  }
}

最后一次编辑于  2021-11-25
回答关注问题邀请回答
收藏

1 个回答

  • Mr.Zhao
    Mr.Zhao
    2021-11-25

    问个寂寞,这么点东西猜不出来的,如何复现

    2021-11-25
    有用
    回复 5
    • Qiu (吉²)
      Qiu (吉²)
      2021-11-25
      npm install p-limit安装好后,在构建npm时报错
      2021-11-25
      回复
    • Mr.Zhao
      Mr.Zhao
      2021-11-25回复Qiu (吉²)
      猜测语法有不兼容的。把这个注释了,报错就换了
      2021-11-25
      回复
    • Qiu (吉²)
      Qiu (吉²)
      2021-11-25
      谢谢,我试试🙏
      2021-11-25
      回复
    • Mr.Zhao
      Mr.Zhao
      2021-11-25回复Qiu (吉²)
      着急的话拎出来,别用npm包
      2021-11-25
      回复
    • Qiu (吉²)
      Qiu (吉²)
      2021-11-25回复Mr.Zhao
      嗯嗯,我单独拎出来了,谢谢🙏
      2021-11-25
      回复
登录 后发表内容