diff --git a/src/selector/asyncSelector.ts b/src/selector/asyncSelector.ts index a814e2f..364523c 100644 --- a/src/selector/asyncSelector.ts +++ b/src/selector/asyncSelector.ts @@ -55,11 +55,15 @@ const asyncSelector = ( const currentPendingVersion = (lastPendingVersion + 1) % Number.MAX_SAFE_INTEGER; lastPendingVersion = currentPendingVersion; - events.requested.emit(); - const status = getAggregatedAsyncStatus(asyncStates); const errors = asyncStates.map((state) => state.getAsync().error).filter(Boolean); + if (status === AsyncStatus.PENDING) { + isLoading = true; + } + + events.requested.emit(); + if (errors.length > 0) { error = new AggregateError(errors); value = undefined; @@ -70,10 +74,6 @@ const asyncSelector = ( return; } - if (status === AsyncStatus.PENDING) { - isLoading === true; - } - if (status === AsyncStatus.FULFILLED) { const values = states.map((state) => state.get()) as StatesValues; diff --git a/tests/selector.test.ts b/tests/selector.test.ts index 7f64b36..1c249e1 100644 --- a/tests/selector.test.ts +++ b/tests/selector.test.ts @@ -183,11 +183,12 @@ test('emits `requested` event when one of async dependencies is requested', asyn expect(mergedState.get()).toEqual('Hello Noname'); setTimeout(() => { - nameState.set(delay(10).then(() => 'Awai')); + nameState.set(delay(20).then(() => 'Awai')); }, 10); await mergedState.events.requested; expect(mergedState.getStatus()).toEqual(AsyncStatus.PENDING); + expect(mergedState.getAsync()).toMatchObject({ isLoading: true }); await delay(5); expect(mergedState.events.fulfilled).resolves.toEqual('Hello Awai'); expect(mergedState.events.changed).resolves.toEqual('Hello Awai');