Skip to content

Commit

Permalink
[promise] promise.all typing improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed May 24, 2023
1 parent 783af8c commit b394fc8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
9 changes: 5 additions & 4 deletions promise.js
Expand Up @@ -27,11 +27,12 @@ export const createEmpty = f => new Promise(f)

/**
* `Promise.all` wait for all promises in the array to resolve and return the result
* @template T
* @param {Array<Promise<T>>} arrp
* @return {Promise<Array<T>>}
* @template {unknown[] | []} PS
*
* @param {PS} ps
* @return {Promise<{ -readonly [P in keyof PS]: Awaited<PS[P]> }>}
*/
export const all = arrp => Promise.all(arrp)
export const all = Promise.all.bind(Promise)

/**
* @param {Error} [reason]
Expand Down
24 changes: 20 additions & 4 deletions promise.test.js
Expand Up @@ -25,9 +25,9 @@ const measureP = (p, min, max) => {
const failsP = p => promise.create((resolve, reject) => p.then(() => reject(error.create('Promise should fail')), resolve))

/**
* @param {t.TestCase} tc
* @param {t.TestCase} _tc
*/
export const testRepeatPromise = async tc => {
export const testRepeatPromise = async _tc => {
t.assert(promise.createEmpty(r => r()).constructor === Promise, 'p.create() creates a Promise')
t.assert(promise.resolve().constructor === Promise, 'p.reject() creates a Promise')
const rejectedP = promise.reject()
Expand All @@ -44,9 +44,9 @@ export const testRepeatPromise = async tc => {
}

/**
* @param {t.TestCase} tc
* @param {t.TestCase} _tc
*/
export const testispromise = tc => {
export const testispromise = _tc => {
t.assert(promise.isPromise(new Promise(() => {})))
t.assert(promise.isPromise(promise.create(() => {})))
const rej = promise.reject()
Expand All @@ -58,3 +58,19 @@ export const testispromise = tc => {
t.assert(promise.isPromise({ then: () => {}, catch: () => {} }))
})
}

/**
* @param {t.TestCase} _tc
*/
export const testTypings = async _tc => {
const ps = await promise.all([promise.resolveWith(4), 'string'])
/**
* @type {number}
*/
const a = ps[0]
/**
* @type {string}
*/
const b = ps[1]
t.assert(typeof a === 'number' && typeof b === 'string')
}

0 comments on commit b394fc8

Please sign in to comment.