Skip to content

Commit 16a6b3b

Browse files
committed
make our own promiseWithResolvers
1 parent ee00174 commit 16a6b3b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/toolkit/src/tests/createAsyncThunk.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { noop } from '@internal/listenerMiddleware/utils'
2-
import { delay } from '@internal/utils'
2+
import { delay, promiseWithResolvers } from '@internal/utils'
33
import type { CreateAsyncThunkFunction, UnknownAction } from '@reduxjs/toolkit'
44
import {
55
configureStore,
@@ -1014,7 +1014,7 @@ describe('dispatch config', () => {
10141014
test('accepts external signal', async () => {
10151015
const asyncThunk = createAsyncThunk('test', async (_: void, { signal }) => {
10161016
signal.throwIfAborted()
1017-
const { promise, reject } = Promise.withResolvers()
1017+
const { promise, reject } = promiseWithResolvers<never>()
10181018
signal.addEventListener('abort', () => reject(signal.reason))
10191019
return promise
10201020
})
@@ -1031,7 +1031,7 @@ describe('dispatch config', () => {
10311031
test('handles already aborted external signal', async () => {
10321032
const asyncThunk = createAsyncThunk('test', async (_: void, { signal }) => {
10331033
signal.throwIfAborted()
1034-
const { promise, reject } = Promise.withResolvers()
1034+
const { promise, reject } = promiseWithResolvers<never>()
10351035
signal.addEventListener('abort', () => reject(signal.reason))
10361036
return promise
10371037
})

packages/toolkit/src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,17 @@ export function getOrInsertComputed<K extends object, V>(
109109

110110
return map.set(key, compute(key)).get(key) as V
111111
}
112+
113+
export function promiseWithResolvers<T>(): {
114+
promise: Promise<T>
115+
resolve: (value: T | PromiseLike<T>) => void
116+
reject: (reason?: any) => void
117+
} {
118+
let resolve: any
119+
let reject: any
120+
const promise = new Promise<T>((res, rej) => {
121+
resolve = res
122+
reject = rej
123+
})
124+
return { promise, resolve, reject }
125+
}

0 commit comments

Comments
 (0)