Skip to content

Commit

Permalink
feat(utils): delay 함수 추가 (#92)
Browse files Browse the repository at this point in the history
* feat(utils): delay 함수 추가

* fix: delay 함수 관련 문서 내에 존재하는 불필요한 함수 수정 완료

* Create long-lobsters-type.md

* fix: 테스트코드 동작시간 단축을 위한 delay함수 검증시간 수정 완료

* refac: useBlockPromiseMultipleClick 테스트코드에서 사용중인 delay함수를 utils에 있는 함수로 교체 완료

---------

Co-authored-by: Gromit (전민재) <[email protected]>
  • Loading branch information
Sangminnn and ssi02014 authored May 1, 2024
1 parent c3bbab6 commit fa308f6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-lobsters-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modern-kit/utils": patch
---

feat(utils): delay 함수 추가
24 changes: 24 additions & 0 deletions docs/docs/utils/common/delay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# delay

주어진 시간만큼 기다린 뒤 다음 동작을 수행할 수 있도록 하는 함수입니다.

setTimeout을 사용하여 특정 시간 뒤의 동작을 정의할 경우, 해당 시간 뒤 동작해야하는 함수 다음에 Promise가 존재한다면 setTimeout은 macroTaskQueue에 속하고 Promise는 microTaskQueue에 속하게 되어 의도한 바와 같이 순서대로의 동작을 보장하지 못할 수 있습니다. delay 함수를 사용한다면 이러한 문제를 해결할 수 있습니다.

<br />

## Interface
```tsx
const delay: (time: number) => Promise<void>
```
## Usage
```ts
import { delay } from '@modern-kit/utils';

const something = () => Promise.resolve()

const doSomethingAfterDelay = async () => {
await delay(1000)
await something()
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { describe, expect, it } from 'vitest';
import { screen, renderHook } from '@testing-library/react';
import { renderSetup } from '../../utils/test/renderSetup';
import { useBlockPromiseMultipleClick } from '.';

const delay = (time: number) => {
return new Promise<void>((resolve) => {
setTimeout(() => resolve(), time);
});
};
import { delay } from '@modern-kit/utils';

beforeEach(() => {
vi.useFakeTimers();
Expand All @@ -29,7 +24,7 @@ describe('useBlockPromiseMultipleClick', () => {

const { user } = renderSetup(
<button onClick={onClick}>TestButton</button>,
{ delay: null }
{ delay: null },
);

const button = screen.getByRole('button');
Expand Down
15 changes: 15 additions & 0 deletions packages/utils/src/common/delay/delay.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';
import { delay } from '.';

describe('delay', () => {
it('should delay the promise by the given time', async () => {
const time = 200;
const start = Date.now();

await delay(time);

const end = Date.now();

expect(end - start).toBeGreaterThanOrEqual(time);
});
});
5 changes: 5 additions & 0 deletions packages/utils/src/common/delay/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const delay = (time: number) => {
return new Promise<void>((resolve) => {
setTimeout(() => resolve(), time);
});
};
1 change: 1 addition & 0 deletions packages/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './abRandom';
export * from './asyncNoop';
export * from './deepCopy';
export * from './deepEqual';
export * from './delay';
export * from './getUniqId';
export * from './getUniqTime';
export * from './getViewportSize';
Expand Down

0 comments on commit fa308f6

Please sign in to comment.