@@ -4,6 +4,7 @@ import asyncWrap from './asyncWrap.mjs'
4
4
import asyncIterableWrap from './asyncIterableWrap.mjs'
5
5
import getQueue from './getQueue.mjs'
6
6
import Queue from './Queue.mjs'
7
+ import reflectStatus from './reflectStatus.mjs'
7
8
8
9
/**
9
10
* Produces a an async iterator that will return each value or `iterable` after having processed them through
@@ -74,11 +75,7 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
74
75
const waitList = new Map ( )
75
76
const addToWaitList = ( identifier , fct ) => {
76
77
const p = ( async ( ) => {
77
- try {
78
- return [ identifier , 'resolved' , await fct ( ) ]
79
- } catch ( e ) {
80
- return [ identifier , 'rejected' , e ]
81
- }
78
+ return [ identifier , await reflectStatus ( fct ) ]
82
79
} ) ( )
83
80
assert ( ! waitList . has ( 'identifier' ) , 'waitList already contains identifier' )
84
81
waitList . set ( identifier , p )
@@ -110,12 +107,11 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
110
107
const removed = scheduledList . delete ( index )
111
108
assert ( removed , 'Couldn\'t find index in scheduledList for removal' )
112
109
113
- const [ state , result ] = await iteratee ( value , index , iterable )
114
- . then ( ( r ) => [ 'resolved' , r ] , ( e ) => [ 'rejected' , e ] )
110
+ const snapshot = await reflectStatus ( ( ) => iteratee ( value , index , iterable ) )
115
111
116
112
scheduledCount -= 1
117
- insertInResults ( index , value , state , result )
118
- if ( state === 'rejected' ) {
113
+ insertInResults ( index , value , snapshot )
114
+ if ( snapshot . status === 'rejected' ) {
119
115
shouldStop = true
120
116
cancelAllScheduled ( ordered ? index : 0 )
121
117
}
@@ -142,10 +138,10 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
142
138
const fetch = ( ) => {
143
139
fetching = true
144
140
addToWaitList ( 'next' , async ( ) => {
145
- const [ state , result ] = await it . next ( ) . then ( ( r ) => [ 'resolved' , r ] , ( e ) => [ 'rejected' , e ] )
141
+ const snapshot = await reflectStatus ( ( ) => it . next ( ) )
146
142
fetching = false
147
- if ( state === 'resolved ' ) {
148
- const { value, done } = result
143
+ if ( snapshot . status === 'fulfilled ' ) {
144
+ const { value, done } = snapshot . value
149
145
if ( ! done ) {
150
146
lastIndexFetched += 1
151
147
assert ( fetchedValue === null , 'fetchedValue should be null' )
@@ -159,19 +155,19 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
159
155
exhausted = true
160
156
lastIndexFetched += 1
161
157
const index = lastIndexFetched
162
- insertInResults ( index , undefined , state , result )
158
+ insertInResults ( index , undefined , snapshot )
163
159
cancelAllScheduled ( ordered ? index : 0 )
164
160
}
165
161
} )
166
162
}
167
163
168
- const insertInResults = ( index , value , state , result ) => {
164
+ const insertInResults = ( index , value , snapshot ) => {
169
165
if ( ordered ) {
170
166
assert ( index - lastIndexHandled - 1 >= 0 , 'invalid index to insert' )
171
167
assert ( results [ index - lastIndexHandled - 1 ] === undefined , 'already inserted result' )
172
- results [ index - lastIndexHandled - 1 ] = { index, value, state , result }
168
+ results [ index - lastIndexHandled - 1 ] = { index, value, snapshot }
173
169
} else {
174
- results . push ( { index, value, state , result } )
170
+ results . push ( { index, value, snapshot } )
175
171
}
176
172
}
177
173
@@ -181,10 +177,10 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
181
177
while ( results . length >= 1 && results [ 0 ] !== undefined ) {
182
178
const result = results . shift ( )
183
179
lastIndexHandled += 1
184
- if ( result . state === 'rejected' ) {
185
- throw result . result
180
+ if ( result . snapshot . status === 'rejected' ) {
181
+ throw result . snapshot . reason
186
182
} else {
187
- yield result . result
183
+ yield result . snapshot . value
188
184
}
189
185
}
190
186
if ( exhausted && lastIndexFetched === lastIndexHandled ) {
0 commit comments