The Promise withResolvers() static method returns an object containing a new Promise object and two functions to resolve or reject it, corresponding to the two parameters passed to the executor of the Promise() constructor.

    
    async function* readableToAsyncIterable(stream) {
      let { promise, resolve, reject } = Promise.withResolvers();
      stream.on("error", (error) => reject(error));
      stream.on("end", () => resolve());
      stream.on("readable", () => resolve());

      while (stream.readable) {
        await promise;
        let chunk;
        while ((chunk = stream.read())) {
          yield chunk;
        }
        ({ promise, resolve, reject } = Promise.withResolvers());
      }
    }