• callback {Function} 要放入队列的函数。

    queueMicrotask() 方法会将微任务放入队列以便调用 callback。 如果 callback 抛出异常,则将会触发 [process 对象][process object]的 'uncaughtException' 事件。

    微任务队列由 V8 进行管理,可以通过与 [process.nextTick()] 队列(由 Node.js 管理)类似的方式进行使用。 在 Node.js 事件循环的每次轮询中,process.nextTick() 队列始终在微任务队列之前执行。

    1. // 在这里,`queueMicrotask()` 用于确保 'load' 事件总是异步地触发,且因此始终如一。
    2. // 在这里使用 `process.nextTick()` 会导致 'load' 事件总是在任何其他 promise 任务之前触发。
    3. DataHandler.prototype.load = async function load(key) {
    4. const hit = this._cache.get(url);
    5. if (hit !== undefined) {
    6. queueMicrotask(() => {
    7. this.emit('load', hit);
    8. });
    9. return;
    10. }
    11. const data = await fetchData(key);
    12. this._cache.set(url, data);
    13. this.emit('load', data);
    14. };