AngularJS - 成功的promise可以返回null数据

Lia*_*yan 2 javascript promise angularjs angular-promise

只是想知道,我正在编写测试用例,并且无法找到关于null是否可以在angularJS promise(而不是名为null的对象)中返回的确定答案

Ben*_*aum 5

是的,就像同步代码一样,承诺可以满足您的任何需求.

//example with Bluebird
Promise.try(function(){
    return null;
}).then(function(result){
    alert(result === null); // true
});
Run Code Online (Sandbox Code Playgroud)

一个promise也可以拒绝null,但它通常不应该因为你应该总是拒绝错误.

在Angular中,使用$ q最简单的例子就是$q.when(null)创建一个null明确履行了值的承诺.