【发布时间】:2018-07-22 23:56:15
【问题描述】:
我定义了一个函数如下:
function getCurrentComponent(){
if($rootRouter._currentInstruction){
return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(function (data) {
return data.component.componentType;
});
}else{
return null;
}
}
为了调用这个函数,我做了如下:
factory.getCurrentComponent().then(function (data) {
...
});
问题是当getCurrentComponent函数返回空值时,会产生如下错误:
无法读取 null 的属性 'then'
我该如何解决这个问题?
编辑:
我忘了说我仅限于使用 ES5,所以我无法使用对象 Promise
【问题讨论】:
-
不上链吗?兑现承诺?
-
返回一个被拒绝的 Promise 而不是
null -
为了避免使用 ES6 承诺得到答案,请避免使用 promise 标签。请改用angular-promise 标签。
标签: javascript angularjs angular-promise ecmascript-5