@@ -3,6 +3,7 @@ import assert from 'nanoassert'
3
3
import asyncWrap from './asyncWrap.mjs'
4
4
import asyncIterableWrap from './asyncIterableWrap.mjs'
5
5
import getQueue from './getQueue.mjs'
6
+ import reflectStatus from './reflectStatus.mjs'
6
7
7
8
/**
8
9
* @ignore
@@ -39,11 +40,7 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
39
40
const identifier = waitListIndex
40
41
waitListIndex += 1
41
42
const p = ( async ( ) => {
42
- try {
43
- return [ identifier , 'resolved' , await fct ( ) ]
44
- } catch ( e ) {
45
- return [ identifier , 'rejected' , e ]
46
- }
43
+ return [ identifier , await reflectStatus ( fct ) ]
47
44
} ) ( )
48
45
assert ( ! waitList . has ( identifier ) , 'waitList already contains identifier' )
49
46
waitList . set ( identifier , p )
@@ -75,12 +72,11 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
75
72
const removed = scheduledList . delete ( index )
76
73
assert ( removed , 'Couldn\'t find index in scheduledList for removal' )
77
74
78
- const [ state , result ] = await iteratee ( value , index , iterable )
79
- . then ( ( r ) => [ 'resolved' , r ] , ( e ) => [ 'rejected' , e ] )
75
+ const snapshot = await reflectStatus ( ( ) => iteratee ( value , index , iterable ) )
80
76
81
77
scheduledCount -= 1
82
- insertInResults ( index , value , state , result )
83
- if ( state === 'rejected' || ( state === 'resolved ' && result ) ) {
78
+ insertInResults ( index , value , snapshot )
79
+ if ( snapshot . status === 'rejected' || ( snapshot . status === 'fulfilled ' && snapshot . value ) ) {
84
80
shouldStop = true
85
81
cancelAllScheduled ( ordered ? index : 0 )
86
82
}
@@ -107,10 +103,10 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
107
103
const fetch = ( ) => {
108
104
fetching = true
109
105
addToWaitList ( async ( ) => {
110
- const [ state , result ] = await it . next ( ) . then ( ( r ) => [ 'resolved' , r ] , ( e ) => [ 'rejected' , e ] )
106
+ const snapshot = await reflectStatus ( ( ) => it . next ( ) )
111
107
fetching = false
112
- if ( state === 'resolved ' ) {
113
- const { value, done } = result
108
+ if ( snapshot . status === 'fulfilled ' ) {
109
+ const { value, done } = snapshot . value
114
110
if ( ! done ) {
115
111
lastIndexFetched += 1
116
112
assert ( fetchedValue === null , 'fetchedValue should be null' )
@@ -124,19 +120,19 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
124
120
exhausted = true
125
121
lastIndexFetched += 1
126
122
const index = lastIndexFetched
127
- insertInResults ( index , undefined , state , result )
123
+ insertInResults ( index , undefined , snapshot )
128
124
cancelAllScheduled ( ordered ? index : 0 )
129
125
}
130
126
} )
131
127
}
132
128
133
- const insertInResults = ( index , value , state , result ) => {
129
+ const insertInResults = ( index , value , snapshot ) => {
134
130
if ( ordered ) {
135
131
assert ( index - lastIndexHandled - 1 >= 0 , 'invalid index to insert' )
136
132
assert ( results [ index - lastIndexHandled - 1 ] === undefined , 'already inserted result' )
137
- results [ index - lastIndexHandled - 1 ] = { index, value, state , result }
133
+ results [ index - lastIndexHandled - 1 ] = { index, value, snapshot }
138
134
} else {
139
- results . push ( { index, value, state , result } )
135
+ results . push ( { index, value, snapshot } )
140
136
}
141
137
}
142
138
@@ -146,9 +142,9 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
146
142
while ( results . length >= 1 && results [ 0 ] !== undefined ) {
147
143
const result = results . shift ( )
148
144
lastIndexHandled += 1
149
- if ( result . state === 'rejected' ) {
150
- throw result . result
151
- } else if ( result . result ) {
145
+ if ( result . snapshot . status === 'rejected' ) {
146
+ throw result . snapshot . reason
147
+ } else if ( result . snapshot . value ) {
152
148
return [ result . index , result . value ]
153
149
}
154
150
}
0 commit comments