Skip to content

Commit

Permalink
feat: add two static methods Promise.any() and Promise.try()
Browse files Browse the repository at this point in the history
  • Loading branch information
calimanco committed Dec 26, 2020
1 parent 05c708b commit e7aa974
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ if (global && typeof global.Promise !== 'function') {
* [x] Promise.reject()
* [x] Promise.deferred()
* [x] Promise.allSettled()
* [ ] Promise.any()
* [ ] Promise.try()
* [x] Promise.any()
* [x] Promise.try()

## 说明

Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ if (global && typeof global.Promise !== 'function') {
* [x] Promise.reject()
* [x] Promise.deferred()
* [x] Promise.allSettled()
* [ ] Promise.any()
* [ ] Promise.try()
* [x] Promise.any()
* [x] Promise.try()

## Explanation

Expand Down
24 changes: 24 additions & 0 deletions src/lib/any.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getArrayRealLen } from '../helpers/util'

export function any(MyPromise: any, promises: PromiseLike<any>[]) {
return new MyPromise(
(resolve: (value: any) => void, reject: (reason: any[]) => void) => {
let result: any[] = []
let promisesLen = promises.length

promises.forEach((promise, index) => {
promise.then(resolve, (reason: any) => {
result[index] = reason

if (getArrayRealLen(result) === promisesLen) {
reject(result)
}
})
})
}
)
}

export default function initAny(MyPromise: any) {
return any.bind(null, MyPromise)
}
9 changes: 9 additions & 0 deletions src/lib/try.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function tryFn(MyPromise: any, fn: () => PromiseLike<any>) {
return new MyPromise((resolve: (arg0: PromiseLike<any>) => void) => {
resolve(fn())
})
}

export default function initTry(MyPromise: any) {
return tryFn.bind(null, MyPromise)
}
6 changes: 5 additions & 1 deletion src/promise-polyfill-plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import initResolve from './lib/resolve'
import initReject from './lib/reject'
import initDeferred from './lib/deferred'
import initAllSettled from './lib/allSettled'
import initTry from './lib/try'
import initAny from './lib/any'

const staticMethods = {
all: initAll,
race: initRace,
resolve: initResolve,
reject: initReject,
deferred: initDeferred,
allSettled: initAllSettled
allSettled: initAllSettled,
try: initTry,
any: initAny
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ export interface MyPromiseStatic extends MyPromiseClassStatic {
allSettled<T>(
promiseArray: Iterable<T | PromiseLike<T>>
): MyPromiseInstance<T[]>
try<T>(fn: () => PromiseLike<T>): MyPromiseInstance<T[]>
any<T>(promiseArray: Iterable<T | PromiseLike<T>>): MyPromiseInstance<T>
}
9 changes: 8 additions & 1 deletion test/MyPromise-init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ describe('init function test', () => {
}
})

it('should not add method', () => {
it('should not be added method', () => {
const newPromise = initPromise(Promise)
expect(newPromise.all).not.toEqual(MyPromise.all)
expect(newPromise.race).not.toEqual(MyPromise.race)
expect(newPromise.resolve).not.toEqual(MyPromise.resolve)
expect(newPromise.reject).not.toEqual(MyPromise.reject)
})

it('should be added method', () => {
const newPromise = initPromise(Promise)
expect(newPromise.deferred).not.toBeUndefined()
expect(newPromise.allSettled).not.toBeUndefined()
expect(newPromise.try).not.toBeUndefined()
})
})
49 changes: 49 additions & 0 deletions test/MyPromise-static.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,52 @@ describe('MyPromise‘s static allSettled function test', () => {
})
})
})

describe('MyPromise‘s static try function test', () => {
it('try function base test', done => {
const onFulfilled = jest.fn()
const database = {
get: () =>
new Promise(resolve => {
resolve('lin')
})
}
function getUsername() {
// here will throw wrong
let a: any = 1
a.run()
return database.get().then(function (userName) {
return userName
})
}

MyPromise.try(() => getUsername())
.then(onFulfilled)
.catch(reason => {
expect(onFulfilled).toHaveBeenCalledTimes(0)
expect(reason).toEqual(expect.any(TypeError))
done()
})
})
})

describe('MyPromise‘s static any function test', () => {
it('should be fulfilled with 123', done => {
MyPromise.any([MyPromise.resolve(123), MyPromise.reject(456)]).then(
value => {
expect(value).toBe(123)
done()
}
)
})
it('should be rejected with a array', done => {
const onFulfilled = jest.fn()
MyPromise.any([MyPromise.reject(123), MyPromise.reject(456)])
.then(onFulfilled)
.catch(reason => {
expect(onFulfilled).toHaveBeenCalledTimes(0)
expect(reason).toEqual([123, 456])
done()
})
})
})

0 comments on commit e7aa974

Please sign in to comment.