Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextTick): add nextTick function overload #8406

Merged
merged 7 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,16 @@ describe('scheduler', () => {
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})

it('nextTick should return promise', async () => {
const fn = vi.fn(() => {
return 1
})

const p = nextTick(fn)

expect(p).toBeInstanceOf(Promise)
expect(await p).toBe(1)
expect(fn).toHaveBeenCalledTimes(1)
})
})
8 changes: 5 additions & 3 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ let currentFlushPromise: Promise<void> | null = null
const RECURSION_LIMIT = 100
type CountMap = Map<SchedulerJob, number>

export function nextTick<T = void>(
export function nextTick(): Promise<void>
export function nextTick<T = void, R = any>(
this: T,
fn?: (this: T) => void
): Promise<void> {
fn: (this: T) => R
): Promise<R extends Promise<infer P> ? P : R>
export function nextTick<T, R>(this: T, fn?: (this: T) => R) {
Alfred-Skyblue marked this conversation as resolved.
Show resolved Hide resolved
const p = currentFlushPromise || resolvedPromise
return fn ? p.then(this ? fn.bind(this) : fn) : p
}
Expand Down