File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ import { expect } from 'chai' ;
2+ import { describe , it } from 'mocha' ;
3+
4+ import { expectPromise } from '../../__testUtils__/expectPromise.js' ;
5+
6+ import { promiseWithResolvers } from '../promiseWithResolvers.js' ;
7+
8+ describe ( 'promiseWithResolvers' , ( ) => {
9+ it ( 'resolves values' , async ( ) => {
10+ const { promise, resolve } = promiseWithResolvers ( ) ;
11+ resolve ( 'foo' ) ;
12+ expect ( await expectPromise ( promise ) . toResolve ( ) ) . to . equal ( 'foo' ) ;
13+ } ) ;
14+
15+ it ( 'rejects values' , async ( ) => {
16+ const { promise, reject } = promiseWithResolvers ( ) ;
17+ const error = new Error ( 'rejected' ) ;
18+ reject ( error ) ;
19+ await expectPromise ( promise ) . toRejectWith ( 'rejected' ) ;
20+ } ) ;
21+ } ) ;
Original file line number Diff line number Diff line change 1+ import type { PromiseOrValue } from './PromiseOrValue.js' ;
2+
3+ /**
4+ * Based on Promise.withResolvers proposal
5+ * https://github.com/tc39/proposal-promise-with-resolvers
6+ */
7+ export function promiseWithResolvers < T > ( ) : {
8+ promise : Promise < T > ;
9+ resolve : ( value : T | PromiseOrValue < T > ) => void ;
10+ reject : ( reason ?: any ) => void ;
11+ } {
12+ // these are assigned synchronously within the Promise constructor
13+ let resolve ! : ( value : T | PromiseOrValue < T > ) => void ;
14+ let reject ! : ( reason ?: any ) => void ;
15+ const promise = new Promise < T > ( ( res , rej ) => {
16+ resolve = res ;
17+ reject = rej ;
18+ } ) ;
19+ return { promise, resolve, reject } ;
20+ }
You can’t perform that action at this time.
0 commit comments