简体   繁体   中英

How do I create a ReadableStream in Microsoft Edge?

Both MDN , caniuse.com , and Microsoft Edge's own "Platform Status" page state that Microsoft Edge supports ReadableStream since build number 16299+ (released 09/26/2017).

However, when I try to create a ReadableStream in the latest Edge (Microsoft Edge 44.17763.1.0, Microsoft EdgeHTML 18.17763) I get the error Function expected .

Both

new ReadableStream()

and

new ReadableStream({
  start: function(controller) {},
  pull: function(controller) {},
  cancel: function() {}
})

throw the Function expected error. Omitting the new also doesn't work.

What am I doing wrong?

You could try to use the following code to read the data in Edge:

function pump(reader, context) {
    return reader.read().then(function (result) {
        if (result.done) {
            console.log('ReadableStreamReader: complete! Received ' + context.receivedLength);
        } else {
            var chunk = result.value;
            console.log('ReadableStreamReader: Partial chunk, chunkSize = ' + chunk.byteLength);

            context.receivedLength += chunk.byteLength;
            return pump(reader, context);
        }
    }).catch(function (e) {
        throw e;
    });
}

function fetchVideo() {
    var url = 'xxxxxxxx';

    var headers = new Headers();
    var param = {
        method: 'GET',
        headers: headers,
        mode: 'cors',
        cache: 'default'
    };

    var context = {
        receivedLength: 0
    };

    fetch(url, param).then(function (res) {
        console.log('Content-Length: ' + res.headers.get('Content-Length'));
        return pump(res.body.getReader(), context);
    }).catch(function (e) {
        throw e;
    });
}

According to the ReadableSream doc example , I reproduce the issue on my Edge 44, but it works well on Edge 42. So, I suppose the issue is related to Edge 44, as a workaround I suggest you could try to downgrade the Edge version to 42 version. And, I will try to feedback this issue to Edge Platform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM