我正在尝试在电子应用程序中运行以下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
知道为什么吗?