From 7305f693b18f8916cc193e70d10d262389bfc0fb Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Thu, 17 Oct 2019 22:47:26 +0300 Subject: [PATCH] refactor(runtime-core): extract promise check into shared (#325) --- packages/runtime-core/src/component.ts | 9 +++------ packages/runtime-core/src/errorHandling.ts | 5 +++-- packages/shared/src/index.ts | 4 ++++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 39abd4f1..9b1fe8a8 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -23,7 +23,8 @@ import { isArray, isObject, NO, - makeMap + makeMap, + isPromise } from '@vue/shared' import { SuspenseBoundary } from './suspense' import { @@ -281,11 +282,7 @@ export function setupStatefulComponent( currentInstance = null currentSuspense = null - if ( - setupResult && - isFunction(setupResult.then) && - isFunction(setupResult.catch) - ) { + if (isPromise(setupResult)) { if (__FEATURE_SUSPENSE__) { // async setup returned Promise. // bail here and wait for re-entry. diff --git a/packages/runtime-core/src/errorHandling.ts b/packages/runtime-core/src/errorHandling.ts index fbe78cca..758f8425 100644 --- a/packages/runtime-core/src/errorHandling.ts +++ b/packages/runtime-core/src/errorHandling.ts @@ -1,6 +1,7 @@ import { VNode } from './vnode' import { ComponentInternalInstance, LifecycleHooks } from './component' import { warn, pushWarningContext, popWarningContext } from './warning' +import { isPromise } from '@vue/shared' // contexts where user provided function may be executed, in addition to // lifecycle hooks. @@ -71,8 +72,8 @@ export function callWithAsyncErrorHandling( args?: any[] ) { const res = callWithErrorHandling(fn, instance, type, args) - if (res != null && !res._isVue && typeof res.then === 'function') { - res.catch((err: any) => { + if (res != null && !res._isVue && isPromise(res)) { + res.catch((err: Error) => { handleError(err, instance, type) }) } diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 6e64483a..4511f4f6 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -41,6 +41,10 @@ export const isSymbol = (val: any): val is symbol => typeof val === 'symbol' export const isObject = (val: any): val is Record => val !== null && typeof val === 'object' +export function isPromise(val: any): val is Promise { + return isObject(val) && isFunction(val.then) && isFunction(val.catch) +} + export const objectToString = Object.prototype.toString export const toTypeString = (value: unknown): string => objectToString.call(value)