How to convert Node.js async streaming callback into an async generator?

前端 未结 3 514
猫巷女王i
猫巷女王i 2021-01-14 11:10

I have a function that streams data in batches via a callback.

Each batch will await the callback function before fetching another batch and the entire function retu

3条回答
  •  清酒与你
    2021-01-14 11:56

    You need a event bucket, here is an example:

    function bucket() {
      const stack = [],
        iterate = bucket();
    
      var next;
    
      async function* bucket() {
        while (true) {
          yield new Promise((res) => {
            if (stack.length > 0) {
              return res(stack.shift());
            }
    
            next = res;
          });
        }
      }
    
      iterate.push = (itm) => {
        if (next) {
          next(itm);
          next = false;
          return;
        }
    
        stack.push(itm);
      }
    
      return iterate;
    }
    
    ;
    (async function() {
      let evts = new bucket();
    
      setInterval(() => {
        evts.push(Date.now());
        evts.push(Date.now() + '++');
      }, 1000);
    
      for await (let evt of evts) {
        console.log(evt);
      }
    })();

提交回复
热议问题