1

理想情况下,我们可以使用该Function.prototype.bind函数来做到这一点。我认为这里也没有一种使用胖箭头功能的明确方法。Ycombinator 魔法?

这是我到目前为止所尝试的:

(function pump () {
  return browserReadableStreamReader.read().then(({ done, value }) => {
    if (done) {
      return this.end()
    }

    this.write(value)
    return pump()
  })
}).bind(this)()
4

1 回答 1

0

这是我所做的:

const { PassThrough } = require('stream')
/**
 * Google Chrome ReadableStream PassThrough implementation
 * @extends PassThrough
 */
class BrowserPassThrough extends PassThrough {
  /**
   * @param {Object} options - options to pass to PassThrough
   * @param {ReadableStreamDefaultReader} browserReadableStreamReader - reader
   */
  constructor (options, browserReadableStreamReader) {
    super(options)
    this.reader = browserReadableStreamReader
    this.pump()
  }

  pump () {
    this.reader.read().then(({ done, value }) => {
      if (done) {
        return this.end()
      }

      this.write(value)
      this.pump()
    })
  }
}
module.exports = BrowserPassThrough
于 2018-09-04T20:02:09.273 回答