Skip to content

Commit 35edc1c

Browse files
committed
refactor: extract isPromise util
1 parent 6e9fcfc commit 35edc1c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Diff for: src/core/util/error.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import config from '../config'
44
import { warn } from './debug'
55
import { inBrowser, inWeex } from './env'
6+
import { isPromise } from 'shared/util'
67

78
export function handleError (err: Error, vm: any, info: string) {
89
if (vm) {
@@ -26,7 +27,7 @@ export function handleError (err: Error, vm: any, info: string) {
2627

2728
export function handlePromiseError (value: any, vm: any, info: string) {
2829
// if value is promise, handle it (a promise must have a then function)
29-
if (value && typeof value.then === 'function' && typeof value.catch === 'function') {
30+
if (isPromise(value)) {
3031
value.catch(e => handleError(e, vm, info))
3132
}
3233
}

Diff for: src/core/vdom/helpers/resolve-async-component.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
isUndef,
88
isTrue,
99
isObject,
10-
hasSymbol
10+
hasSymbol,
11+
isPromise
1112
} from 'core/util/index'
1213

1314
import { createEmptyVNode } from 'core/vdom/vnode'
@@ -95,12 +96,12 @@ export function resolveAsyncComponent (
9596
const res = factory(resolve, reject)
9697

9798
if (isObject(res)) {
98-
if (typeof res.then === 'function') {
99+
if (isPromise(res)) {
99100
// () => Promise
100101
if (isUndef(factory.resolved)) {
101102
res.then(resolve, reject)
102103
}
103-
} else if (isDef(res.component) && typeof res.component.then === 'function') {
104+
} else if (isPromise(res.component)) {
104105
res.component.then(resolve, reject)
105106

106107
if (isDef(res.error)) {

Diff for: src/shared/util.js

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ export function isValidArrayIndex (val: any): boolean {
7171
return n >= 0 && Math.floor(n) === n && isFinite(val)
7272
}
7373

74+
export function isPromise (val: any): boolean {
75+
return (
76+
isDef(val) &&
77+
typeof val.then === 'function' &&
78+
typeof val.catch === 'function'
79+
)
80+
}
81+
7482
/**
7583
* Convert a value to a string that is actually rendered.
7684
*/

0 commit comments

Comments
 (0)