Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as _ from 'radashi'
import { bench, describe } from 'vitest'

describe('isPromise', () => {
describe('isPromiseLike', () => {
bench('with Promise', () => {
_.isPromise(new Promise(res => res(0)))
_.isPromiseLike(new Promise(res => res(0)))
})

bench('with Promise-like', () => {
_.isPromise({
_.isPromiseLike({
// biome-ignore lint/suspicious/noThenProperty:
then: () => {},
})
})

bench('with non-Promise', () => {
_.isPromise(22)
_.isPromiseLike(22)
})
})
16 changes: 8 additions & 8 deletions docs/typed/isPromise.mdx → docs/typed/isPromiseLike.mdx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
title: isPromise
title: isPromiseLike
description: 'Determine if a value is a Promise or has a `then` method'
since: 12.1.0
---

### Usage

The `isPromise` function checks if a value is "Promise-like" by determining if it has a `then` method.
The `isPromiseLike` function checks if a value is "Promise-like" by determining if it has a `then` method.

```ts
import * as _ from 'radashi'

_.isPromise({ then: () => {} }) // => true
_.isPromise(new Promise(() => {})) // => true
_.isPromise(Promise.resolve(1)) // => true
_.isPromise(Promise.reject(new Error('nope'))) // => true
_.isPromiseLike({ then: () => {} }) // => true
_.isPromiseLike(new Promise(() => {})) // => true
_.isPromiseLike(Promise.resolve(1)) // => true
_.isPromiseLike(Promise.reject(new Error('nope'))) // => true

_.isPromise('hello') // => false
_.isPromise({}) // => false
_.isPromiseLike('hello') // => false
_.isPromiseLike({}) // => false
```

This approach is useful for identifying objects that conform to the Promise interface without actually being instances of `Promise`. It's particularly helpful in scenarios where:
Expand Down
4 changes: 2 additions & 2 deletions src/async/tryit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPromise, type Result, type ResultPromise } from 'radashi'
import { isPromiseLike, type Result, type ResultPromise } from 'radashi'

/**
* The result of a `tryit` function.
Expand Down Expand Up @@ -41,7 +41,7 @@ export function tryit<
return (...args): any => {
try {
const result = func(...args)
return isPromise(result)
return isPromiseLike(result)
? result.then(
value => [undefined, value],
err => [err, undefined],
Expand Down
2 changes: 1 addition & 1 deletion src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export * from './typed/isNumber.ts'
export * from './typed/isObject.ts'
export * from './typed/isPlainObject.ts'
export * from './typed/isPrimitive.ts'
export * from './typed/isPromise.ts'
export * from './typed/isPromiseLike.ts'
export * from './typed/isRegExp.ts'
export * from './typed/isResult.ts'
export * from './typed/isResultErr.ts'
Expand Down
17 changes: 0 additions & 17 deletions src/typed/isPromise.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/typed/isPromiseLike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isFunction } from 'radashi'

/**
* Returns true if the value is a Promise or has a `then` method.
*
* @see https://radashi.js.org/reference/typed/isPromiseLike
* @example
* ```ts
* isPromiseLike(Promise.resolve(1)) // => true
* isPromiseLike({ then() {} }) // => true
* isPromiseLike(1) // => false
* ```
* @version 12.1.0
*/
export function isPromiseLike(value: any): value is PromiseLike<unknown> {
return !!value && isFunction(value.then)
}
24 changes: 0 additions & 24 deletions tests/typed/isPromise.test.ts

This file was deleted.

24 changes: 24 additions & 0 deletions tests/typed/isPromiseLike.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as _ from 'radashi'

describe('isPromise', () => {
test('return true for Promise-like values', () => {
expect(_.isPromiseLike(new Promise(() => {}))).toBeTruthy()
expect(_.isPromiseLike(Promise.resolve(1))).toBeTruthy()
expect(_.isPromiseLike((async () => {})())).toBeTruthy()
// biome-ignore lint/suspicious/noThenProperty:
expect(_.isPromiseLike({ then: () => {} })).toBeTruthy()
})
test('return false for non-Promise-like values', () => {
expect(_.isPromiseLike(22)).toBeFalsy()
expect(_.isPromiseLike({ name: 'x' })).toBeFalsy()
expect(_.isPromiseLike('abc')).toBeFalsy()
expect(_.isPromiseLike(String('abc'))).toBeFalsy()
expect(_.isPromiseLike([1, 2, 3])).toBeFalsy()
expect(_.isPromiseLike(function work() {})).toBeFalsy()
expect(_.isPromiseLike(() => {})).toBeFalsy()
expect(_.isPromiseLike(Symbol(''))).toBeFalsy()
expect(_.isPromiseLike(Symbol('hello'))).toBeFalsy()
// biome-ignore lint/suspicious/noThenProperty:
expect(_.isPromiseLike({ then: 2 })).toBeFalsy()
})
})
Loading