11'use strict' ;
22
33const {
4+ PromisePrototypeThen,
45 SymbolAsyncIterator,
56 SymbolIterator
67} = primordials ;
@@ -42,9 +43,6 @@ function from(Readable, iterable, opts) {
4243 // being called before last iteration completion.
4344 let reading = false ;
4445
45- // Flag for when iterator needs to be explicitly closed.
46- let needToClose = false ;
47-
4846 readable . _read = function ( ) {
4947 if ( ! reading ) {
5048 reading = true ;
@@ -53,18 +51,23 @@ function from(Readable, iterable, opts) {
5351 } ;
5452
5553 readable . _destroy = function ( error , cb ) {
56- if ( needToClose ) {
57- needToClose = false ;
58- close ( ) . then (
59- ( ) => process . nextTick ( cb , error ) ,
60- ( e ) => process . nextTick ( cb , error || e ) ,
61- ) ;
62- } else {
63- cb ( error ) ;
64- }
54+ PromisePrototypeThen (
55+ close ( error ) ,
56+ ( ) => process . nextTick ( cb , error ) , // nextTick is here in case cb throws
57+ ( e ) => process . nextTick ( cb , e || error ) ,
58+ ) ;
6559 } ;
6660
67- async function close ( ) {
61+ async function close ( error ) {
62+ const hadError = ( error !== undefined ) && ( error !== null ) ;
63+ const hasThrow = typeof iterator . throw === 'function' ;
64+ if ( hadError && hasThrow ) {
65+ const { value, done } = await iterator . throw ( error ) ;
66+ await value ;
67+ if ( done ) {
68+ return ;
69+ }
70+ }
6871 if ( typeof iterator . return === 'function' ) {
6972 const { value } = await iterator . return ( ) ;
7073 await value ;
@@ -73,13 +76,9 @@ function from(Readable, iterable, opts) {
7376
7477 async function next ( ) {
7578 try {
76- needToClose = false ;
7779 const { value, done } = await iterator . next ( ) ;
78- needToClose = ! done ;
7980 if ( done ) {
8081 readable . push ( null ) ;
81- } else if ( readable . destroyed ) {
82- await close ( ) ;
8382 } else {
8483 const res = await value ;
8584 if ( res === null ) {
0 commit comments