>edit我成功地解决了它:我用了defer.resolve(SomeFunction)
有一种方法可以在$q中返回函数,defer.resolve?
我已经试过了:
function SomeFunctino(){
return $q(
getfromDB.then(functino(result) = {
...
resolve()
}).catch(functino (error) {
console.log(error);
reject();
});
);
}
funcion getPromise(){
let defer = $q.defer();
defer.resolve = SomeFunction //the function return promise
return defer.promise;
}
let promise = getPromise();
promise.then((value) => { //value = undefined
value.then(() => {...}
});
当我这样做时,值是未定义的(如果我返回字符串而不是函数,它的工作(。
使用 $q.when
返回一个用函数解析的承诺:
angular.module("app",[])
.run(function($q) {
function someFunction (x) {
return x*x;
}
var promise = $q.when(someFunction);
promise.then(function (fn) {
console.log(fn(5)); // 25
});
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
</body>