diff --git a/packages/avet-utils/lib/component.js b/packages/avet-utils/lib/component.js index 8883d32..a6e2b35 100644 --- a/packages/avet-utils/lib/component.js +++ b/packages/avet-utils/lib/component.js @@ -2,23 +2,32 @@ function getDisplayName(Component) { return Component.displayName || Component.name || 'UnKnown'; } +function isPromise(obj) { + return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; +} + function loadGetInitialProps(Component, ctx) { return new Promise(function(resolve, reject) { if (!Component.getInitialProps) return resolve({}); - Component.getInitialProps(ctx).then(function(props) { - if (!props && (!ctx.res || !ctx.res.finished)) { - const compName = getDisplayName(Component); - const message = `"${ - compName - }.getInitialProps()" should resolve to an object. But found "${ - props - }" instead.`; - return reject(message); - } + const props = Component.getInitialProps(ctx) + if (isPromise(props)) { + props.then(function(p) { + if (!p && (!ctx.res || !ctx.res.finished)) { + const compName = getDisplayName(Component); + const message = `"${ + compName + }.getInitialProps()" should resolve to an object. But found "${ + p + }" instead.`; + return reject(message); + } - return resolve(props); - }) + return resolve(p); + }); + } else { + resolve(props); + } }) }