-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# isPromise | ||
|
||
指定された値が `Promise` かどうかを確認します。 | ||
|
||
この関数は、提供された値が `Promise` のインスタンスかどうかをテストします。 | ||
値が `Promise` の場合は `true` を返し、そうでない場合は `false` を返します。 | ||
|
||
この関数は、TypeScript で型述語としても機能し、引数の型を `Promise` に狭めます。 | ||
|
||
## インターフェース | ||
|
||
```typescript | ||
function isPromise(value: unknown): value is Promise<any>; | ||
``` | ||
|
||
### パラメータ | ||
|
||
- `value` (`unknown`): `Promise`かどうか確認する値。 | ||
|
||
### 戻り値 | ||
|
||
(`value is Promise<any>`): 値が`Promise`の場合は`true`、そうでない場合は`false`。 | ||
|
||
## 例 | ||
|
||
```typescript | ||
const value1 = new Promise(resolve => resolve()); | ||
const value2 = {}; | ||
const value3 = 123; | ||
|
||
console.log(isPromise(value1)); // true | ||
console.log(isPromise(value2)); // false | ||
console.log(isPromise(value3)); // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# isPromise | ||
|
||
주어진 값이 `Promise`의 인스턴스인지 확인해요. | ||
|
||
값이 `Promise`이면 `true`, 아니면 `false`를 반환해요. | ||
|
||
TypeScript의 타입 가드로 주로 사용되는데요, 파라미터로 주어진 값을 `Promise`인 타입으로 좁힐 수 있어요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function isPromise(value: unknown): value is Promise<any>; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `value` (`unknown`): `Promise`인지 확인할 값. | ||
|
||
### 반환 값 | ||
|
||
(`value is Promise<any>`): `value`가 `Promise`이면 `true`, 그렇지 않으면 `false`. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
const value1 = new Promise((resolve) => resolve()); | ||
const value2 = {}; | ||
const value3 = 123; | ||
|
||
console.log(isPromise(value1)); // true | ||
console.log(isPromise(value2)); // false | ||
console.log(isPromise(value3)); // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# isPromise | ||
|
||
Checks if a given value is `Promise`. | ||
|
||
This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Promise`. | ||
|
||
## Signature | ||
|
||
```typescript | ||
function isPromise(value: unknown): value is Promise<any>; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `value` (`unknown`): The value to check if it is a `Promise`. | ||
|
||
### Returns | ||
|
||
(`value is Promise<any>`): Returns `true` if `value` is a `Promise`, else `false`. | ||
|
||
## Examples | ||
|
||
```typescript | ||
const value1 = new Promise((resolve) => resolve()); | ||
const value2 = {}; | ||
const value3 = 123; | ||
|
||
console.log(isPromise(value1)); // true | ||
console.log(isPromise(value2)); // false | ||
console.log(isPromise(value3)); // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# isPromise | ||
|
||
检查给定值是否为 `Promise`。 | ||
|
||
此函数测试提供的值是否为 `Promise` 的实例。 | ||
如果值是 `Promise`,则返回 `true`,否则返回 `false`。 | ||
|
||
此函数还可以作为 TypeScript 中的类型谓词,将参数的类型缩小为 `Promise`。 | ||
|
||
## 签名 | ||
|
||
```typescript | ||
function isPromise(value: unknown): value is Promise<any>; | ||
``` | ||
|
||
### 参数 | ||
|
||
- `value` (`unknown`): 检查是否为`Promise`的值。 | ||
|
||
### 返回值 | ||
|
||
(`value is Promise<any>`): 如果`value`是`Promise`,则返回`true`,否则返回`false`。 | ||
|
||
## 示例 | ||
|
||
```typescript | ||
const value1 = new Promise(resolve => resolve()); | ||
const value2 = {}; | ||
const value3 = 123; | ||
|
||
console.log(isPromise(value1)); // true | ||
console.log(isPromise(value2)); // false | ||
console.log(isPromise(value3)); // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { isPromise } from './isPromise'; | ||
|
||
describe('isPromise', () => { | ||
it('returns true if the value is a Promise', () => { | ||
expect(isPromise(new Promise<void>(resolve => resolve()))).toBe(true); | ||
}); | ||
|
||
it('returns false if the value is not a Promise', () => { | ||
expect(isPromise(null)).toBe(false); | ||
expect(isPromise('')).toBe(false); | ||
expect(isPromise(123)).toBe(false); | ||
expect(isPromise({})).toBe(false); | ||
expect(isPromise([])).toBe(false); | ||
expect(isPromise(new Map())).toBe(false); | ||
expect(isPromise(new Set())).toBe(false); | ||
expect(isPromise(new WeakSet())).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Checks if a given value is `Promise`. | ||
* | ||
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Promise`. | ||
* | ||
* @param {unknown} value The value to check if it is a `Promise`. | ||
* @returns {value is Promise<any>} Returns `true` if `value` is a `Promise`, else `false`. | ||
* | ||
* @example | ||
* const value1 = new Promise((resolve) => resolve()); | ||
* const value2 = {}; | ||
* const value3 = 123; | ||
* | ||
* console.log(isPromise(value1)); // true | ||
* console.log(isPromise(value2)); // false | ||
* console.log(isPromise(value3)); // false | ||
*/ | ||
export function isPromise(value: unknown): value is Promise<any> { | ||
return value instanceof Promise; | ||
} |