ReadableStream with async reader

by: Ирина Филиппова     Published on: 21 June 2019

Views: 21

0    0   

Fast Download

Description :

Пример создания потока на чтение ( ReadableStream )https://github.com/garevna/js-course/wiki/ReadableStream___________________________________________________const textStream = new ReadableStream({ start(controller) { let messages = [ "How do you use the Streams API’s readable stream functionality?", "The Fetch API allows you to fetch resources across the network", "The body property is a simple getter exposing the body contents as a readable stream", "You can consume Fetch Body objects as streams and create your own custom readable streams", "The ReadableStream interface of the Streams API represents a readable stream of byte data", "The Fetch API offers a concrete instance of a ReadableStream through the body property of a Response object" ] while ( messages.length ) { controller.enqueue( messages.shift() ) } controller.close() }})___________________________________________________Чтение потока с помощью асинхронной функции___________________________________________________async function readStream ( stream, container ) { const reader = stream.getReader() do { var { done, value } = await reader.read() container.putChank ( done ? "" : value ) } while ( !done )}___________________________________________________Создание контейнера для вывода данных___________________________________________________const messageBox = document.body.appendChild ( document.createElement ( "section" ))messageBox.putChank = function ( chank ) { this.appendChild ( document.createElement( 'p' ) ).textContent = chank}___________________________________________________Вызов функции чтения данных из потока___________________________________________________readStream ( textStream, messageBox )