Skip to content

Commit

Permalink
make our own promiseWithResolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Feb 24, 2025
1 parent ee00174 commit 16a6b3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/toolkit/src/tests/createAsyncThunk.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { noop } from '@internal/listenerMiddleware/utils'
import { delay } from '@internal/utils'
import { delay, promiseWithResolvers } from '@internal/utils'
import type { CreateAsyncThunkFunction, UnknownAction } from '@reduxjs/toolkit'
import {
configureStore,
Expand Down Expand Up @@ -1014,7 +1014,7 @@ describe('dispatch config', () => {
test('accepts external signal', async () => {
const asyncThunk = createAsyncThunk('test', async (_: void, { signal }) => {
signal.throwIfAborted()
const { promise, reject } = Promise.withResolvers()
const { promise, reject } = promiseWithResolvers<never>()
signal.addEventListener('abort', () => reject(signal.reason))
return promise
})
Expand All @@ -1031,7 +1031,7 @@ describe('dispatch config', () => {
test('handles already aborted external signal', async () => {
const asyncThunk = createAsyncThunk('test', async (_: void, { signal }) => {
signal.throwIfAborted()
const { promise, reject } = Promise.withResolvers()
const { promise, reject } = promiseWithResolvers<never>()
signal.addEventListener('abort', () => reject(signal.reason))
return promise
})
Expand Down
14 changes: 14 additions & 0 deletions packages/toolkit/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ export function getOrInsertComputed<K extends object, V>(

return map.set(key, compute(key)).get(key) as V
}

export function promiseWithResolvers<T>(): {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
} {
let resolve: any
let reject: any
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return { promise, resolve, reject }
}

0 comments on commit 16a6b3b

Please sign in to comment.