]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(runtime-core): extract promise check into shared (#325)
authorDmitry Sharshakov <d3dx12.xx@gmail.com>
Thu, 17 Oct 2019 19:47:26 +0000 (22:47 +0300)
committerEvan You <yyx990803@gmail.com>
Thu, 17 Oct 2019 19:47:26 +0000 (15:47 -0400)
packages/runtime-core/src/component.ts
packages/runtime-core/src/errorHandling.ts
packages/shared/src/index.ts

index 39abd4f127b5e7b02f2b901e3b7b25f0e4504905..9b1fe8a82f4b38f32f7f6bd38859be99477cc5ca 100644 (file)
@@ -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.
index fbe78cca68f74f2718e56ed94e67f9f678afac6d..758f8425e3f5239e294f001657d38ef77a415ae5 100644 (file)
@@ -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)
     })
   }
index 6e64483a5a72e995f87dd29cacd2f192df67cde9..4511f4f637f5f88e602edbfd1978d1e2d591cd7a 100644 (file)
@@ -41,6 +41,10 @@ export const isSymbol = (val: any): val is symbol => typeof val === 'symbol'
 export const isObject = (val: any): val is Record<any, any> =>
   val !== null && typeof val === 'object'
 
+export function isPromise<T = any>(val: any): val is Promise<T> {
+  return isObject(val) && isFunction(val.then) && isFunction(val.catch)
+}
+
 export const objectToString = Object.prototype.toString
 export const toTypeString = (value: unknown): string =>
   objectToString.call(value)