Skip to content

Commit 666d774

Browse files
committed
Added usage of reflectStatus in findInternal
1 parent 1fcff3d commit 666d774

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/findInternal.mjs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import assert from 'nanoassert'
33
import asyncWrap from './asyncWrap.mjs'
44
import asyncIterableWrap from './asyncIterableWrap.mjs'
55
import getQueue from './getQueue.mjs'
6+
import reflectStatus from './reflectStatus.mjs'
67

78
/**
89
* @ignore
@@ -39,11 +40,7 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
3940
const identifier = waitListIndex
4041
waitListIndex += 1
4142
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)]
4744
})()
4845
assert(!waitList.has(identifier), 'waitList already contains identifier')
4946
waitList.set(identifier, p)
@@ -75,12 +72,11 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
7572
const removed = scheduledList.delete(index)
7673
assert(removed, 'Couldn\'t find index in scheduledList for removal')
7774

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))
8076

8177
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)) {
8480
shouldStop = true
8581
cancelAllScheduled(ordered ? index : 0)
8682
}
@@ -107,10 +103,10 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
107103
const fetch = () => {
108104
fetching = true
109105
addToWaitList(async () => {
110-
const [state, result] = await it.next().then((r) => ['resolved', r], (e) => ['rejected', e])
106+
const snapshot = await reflectStatus(() => it.next())
111107
fetching = false
112-
if (state === 'resolved') {
113-
const { value, done } = result
108+
if (snapshot.status === 'fulfilled') {
109+
const { value, done } = snapshot.value
114110
if (!done) {
115111
lastIndexFetched += 1
116112
assert(fetchedValue === null, 'fetchedValue should be null')
@@ -124,19 +120,19 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
124120
exhausted = true
125121
lastIndexFetched += 1
126122
const index = lastIndexFetched
127-
insertInResults(index, undefined, state, result)
123+
insertInResults(index, undefined, snapshot)
128124
cancelAllScheduled(ordered ? index : 0)
129125
}
130126
})
131127
}
132128

133-
const insertInResults = (index, value, state, result) => {
129+
const insertInResults = (index, value, snapshot) => {
134130
if (ordered) {
135131
assert(index - lastIndexHandled - 1 >= 0, 'invalid index to insert')
136132
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 }
138134
} else {
139-
results.push({ index, value, state, result })
135+
results.push({ index, value, snapshot })
140136
}
141137
}
142138

@@ -146,9 +142,9 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
146142
while (results.length >= 1 && results[0] !== undefined) {
147143
const result = results.shift()
148144
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) {
152148
return [result.index, result.value]
153149
}
154150
}

src/mapGenerator.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
7878
const identifier = waitListIndex
7979
waitListIndex += 1
8080
const p = (async () => {
81-
const snapshot = await reflectStatus(fct)
82-
return [identifier, snapshot]
81+
return [identifier, await reflectStatus(fct)]
8382
})()
8483
assert(!waitList.has(identifier), 'waitList contains identifier')
8584
waitList.set(identifier, p)

0 commit comments

Comments
 (0)