电子:为什么这段代码在html文件中运行而在renderer.js中运行?

时间:2016-12-18 00:02:50

标签: javascript ecmascript-6 electron

我正在尝试在电子应用程序中运行以下JavaScript。

function consume(reader) {
  var total = 0
  return new Promise((resolve, reject) => {
    function pump() {
      reader.read().then(({done, value}) => {
        if (done) {
          resolve()
          return
        }
        total += value.byteLength
        console.log(`received ${value.byteLength} bytes (${total} bytes in total)`)
        pump()
      }).catch(reject)
    }
    pump()
  })
}
fetch("http://i.imgur.com/76oAXmW.jpg")
  .then(res => consume(res.body.getReader()))
  .then(() => console.log("consumed the entire body without keeping the whole thing in memory!"))
  .catch(e => console.log("something went wrong: " + e))

当我将其包含在<script>标记内时,它可以正常运行,但当我尝试在renderer.js内运行时,我收到错误res.body.getReader is not a function

知道为什么吗?

0 个答案:

没有答案