فهرست منبع

refactor: extract isPromise util

Evan You 7 سال پیش
والد
کامیت
35edc1c1e2
3فایلهای تغییر یافته به همراه14 افزوده شده و 4 حذف شده
  1. 2 1
      src/core/util/error.js
  2. 4 3
      src/core/vdom/helpers/resolve-async-component.js
  3. 8 0
      src/shared/util.js

+ 2 - 1
src/core/util/error.js

@@ -3,6 +3,7 @@
 import config from '../config'
 import { warn } from './debug'
 import { inBrowser, inWeex } from './env'
+import { isPromise } from 'shared/util'
 
 export function handleError (err: Error, vm: any, info: string) {
   if (vm) {
@@ -26,7 +27,7 @@ export function handleError (err: Error, vm: any, info: string) {
 
 export function handlePromiseError (value: any, vm: any, info: string) {
   // if value is promise, handle it (a promise must have a then function)
-  if (value && typeof value.then === 'function' && typeof value.catch === 'function') {
+  if (isPromise(value)) {
     value.catch(e => handleError(e, vm, info))
   }
 }

+ 4 - 3
src/core/vdom/helpers/resolve-async-component.js

@@ -7,7 +7,8 @@ import {
   isUndef,
   isTrue,
   isObject,
-  hasSymbol
+  hasSymbol,
+  isPromise
 } from 'core/util/index'
 
 import { createEmptyVNode } from 'core/vdom/vnode'
@@ -95,12 +96,12 @@ export function resolveAsyncComponent (
     const res = factory(resolve, reject)
 
     if (isObject(res)) {
-      if (typeof res.then === 'function') {
+      if (isPromise(res)) {
         // () => Promise
         if (isUndef(factory.resolved)) {
           res.then(resolve, reject)
         }
-      } else if (isDef(res.component) && typeof res.component.then === 'function') {
+      } else if (isPromise(res.component)) {
         res.component.then(resolve, reject)
 
         if (isDef(res.error)) {

+ 8 - 0
src/shared/util.js

@@ -71,6 +71,14 @@ export function isValidArrayIndex (val: any): boolean {
   return n >= 0 && Math.floor(n) === n && isFinite(val)
 }
 
+export function isPromise (val: any): boolean {
+  return (
+    isDef(val) &&
+    typeof val.then === 'function' &&
+    typeof val.catch === 'function'
+  )
+}
+
 /**
  * Convert a value to a string that is actually rendered.
  */