--- title: ReadableStream slug: Web/API/ReadableStream translation_of: Web/API/ReadableStream --- <p>{{APIRef("Streams")}}</p> <p><span class="seoSummary"><a href="https://developer.mozilla.org/ko/docs/Web/API/Streams_API">Streams API</a>의 <code>ReadableStream</code> 인터페이스는 바이트 데이터를 읽을수 있는 스트림을 제공합니다. <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a>는 Response 객체의 body 속성을 통하여 <code>ReadableStream</code>의 구체적인 인스턴스를 제공합니다.</span></p> <h2 id="Constructor">Constructor</h2> <dl> <dt>{{domxref("ReadableStream.ReadableStream", "ReadableStream()")}}</dt> <dd>읽을수 있는 스트림 객체를 생성하고 리턴합니다.</dd> </dl> <h2 id="Properties">Properties</h2> <dl> <dt>{{domxref("ReadableStream.locked")}} {{readonlyInline}}</dt> <dd><code>locked</code>는 Readable stream이 reader에 고정되어 있는지(<a href="https://streams.spec.whatwg.org/#locked-to-a-reader">locaked to a reader</a>) 확인하는 getter 입니다.</dd> </dl> <h2 id="Methods">Methods</h2> <dl> <dt>{{domxref("ReadableStream.cancel()")}}</dt> <dd> <p>스트림을 취소하여, 소비자가 스트림에 대해 관심이 없음을 알립니다. The supplied reason argument will be given to the underlying source, which may or may not use it.</p> </dd> <dt>{{domxref("ReadableStream.getReader()")}}</dt> <dd>Reader를 만들고 스트림을 그 Reader에 고정 시킵니다. 스트림이 고정되어 있는 동안에는 다른 Reader를 얻을수 없습니다.</dd> <dt>{{domxref("ReadableStream.pipeThrough()")}}</dt> <dd>Provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.</dd> <dt>{{domxref("ReadableStream.pipeTo()")}}</dt> <dd> <p>인자로 넘기는 {{domxref("WritableStream")}}과 현재의 ReadableStream을 연결하고 프로미스를 리턴합니다. 이 프로미스는 파이핑 프로세스가 성공적으로 완료될때 fullfil되며 애러가 발생했을때 reject됩니다.</p> </dd> <dt>{{domxref("ReadableStream.tee()")}}</dt> <dd>The <code>tee</code> method <a href="https://streams.spec.whatwg.org/#tee-a-readable-stream" id="ref-for-tee-a-readable-stream②">tees</a> this readable stream, returning a two-element array containing the two resulting branches as new {{domxref("ReadableStream")}} instances. Each of those streams receives the same incoming data.</dd> </dl> <h2 id="Examples">Examples</h2> <p>아래 예시에서, 다른 리소스에서 fetch된 HTML 조각들을 스트림 하기위해 가공의 {{domxref("Response")}}를 만듭니다. 이것은{{domxref("Uint8Array")}}로 구성된 {{domxref("ReadableStream")}} 의 사용법을 보여줍니다.</p> <pre class="brush: js">fetch("https://www.example.org/").then((response) => { const reader = response.body.getReader(); const stream = new ReadableStream({ start(controller) { // 아래 함수는 각 data chunck를 다룬다. function push() { // "done"은 Boolean 이며 value는 "Uint8Array 이다." reader.read().then(({ done, value }) => { // 더이상 읽은 데이터가 없는가? if (done) { // 브라우저에게 데이터 전달이 끝났다고 알린다. controller.close(); return; } // 데이터를 얻고 컨트롤러를 통하여 그 데이터를 브라우저에 넘긴다. controller.enqueue(value); push(); }); }; push(); } }); return new Response(stream, { headers: { "Content-Type": "text/html" } }); }); </pre> <h2 id="Specifications">Specifications</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('Streams','#rs-class','ReadableStream')}}</td> <td>{{Spec2('Streams')}}</td> <td>Initial definition</td> </tr> </tbody> </table> <h2 id="Browser_compatibility">Browser compatibility</h2> <div> <p>{{Compat("api.ReadableStream")}}</p> </div> <h2 id="See_Also">See Also</h2> <ul> <li><a href="https://whatwg-stream-visualizer.glitch.me/">WHATWG Stream Visualiser</a>, for a basic visualisation of readable, writable, and transform streams.</li> </ul>