Skip to content

Commit 7305f69

Browse files
dssengyyx990803
authored andcommitted
refactor(runtime-core): extract promise check into shared (vuejs#325)
1 parent bb9dca2 commit 7305f69

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

packages/runtime-core/src/component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
isArray,
2424
isObject,
2525
NO,
26-
makeMap
26+
makeMap,
27+
isPromise
2728
} from '@vue/shared'
2829
import { SuspenseBoundary } from './suspense'
2930
import {
@@ -281,11 +282,7 @@ export function setupStatefulComponent(
281282
currentInstance = null
282283
currentSuspense = null
283284

284-
if (
285-
setupResult &&
286-
isFunction(setupResult.then) &&
287-
isFunction(setupResult.catch)
288-
) {
285+
if (isPromise(setupResult)) {
289286
if (__FEATURE_SUSPENSE__) {
290287
// async setup returned Promise.
291288
// bail here and wait for re-entry.

packages/runtime-core/src/errorHandling.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { VNode } from './vnode'
22
import { ComponentInternalInstance, LifecycleHooks } from './component'
33
import { warn, pushWarningContext, popWarningContext } from './warning'
4+
import { isPromise } from '@vue/shared'
45

56
// contexts where user provided function may be executed, in addition to
67
// lifecycle hooks.
@@ -71,8 +72,8 @@ export function callWithAsyncErrorHandling(
7172
args?: any[]
7273
) {
7374
const res = callWithErrorHandling(fn, instance, type, args)
74-
if (res != null && !res._isVue && typeof res.then === 'function') {
75-
res.catch((err: any) => {
75+
if (res != null && !res._isVue && isPromise(res)) {
76+
res.catch((err: Error) => {
7677
handleError(err, instance, type)
7778
})
7879
}

packages/shared/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export const isSymbol = (val: any): val is symbol => typeof val === 'symbol'
4141
export const isObject = (val: any): val is Record<any, any> =>
4242
val !== null && typeof val === 'object'
4343

44+
export function isPromise<T = any>(val: any): val is Promise<T> {
45+
return isObject(val) && isFunction(val.then) && isFunction(val.catch)
46+
}
47+
4448
export const objectToString = Object.prototype.toString
4549
export const toTypeString = (value: unknown): string =>
4650
objectToString.call(value)

0 commit comments

Comments
 (0)