AngularJS : Seqential Promise chain
AngularJS : Seqential Promise chain
函数one
将值传递给two
,然后two
将值传递到three
。这些函数中的任何一个都可能需要花费任何时间来返回数据。我如何让他们等待价值,而不是匆匆忙忙地打印undefined
。
var deferred = $q.defer();
var one = function (msg) {
$timeout(function () {
console.log(msg);
return "pass this to two";
}, 2000);
};
var two = function (msg) {
console.log(msg);
return "pass this to three";
};
var three = function (msg) {
console.log(msg);
};
deferred.promise
.then(one)
.then(two)
.then(three);
deferred.resolve("pass this to one");
您需要return
从每个执行异步操作的函数中获得一个promise。
在您的情况下,one
函数返回undefined
,而它需要在超时后返回您为"pass this to two"
值创建的promise:
function one (msg) {
return $timeout(function () {
//^^^^^^
console.log(msg);
return "pass this to two";
}, 2000);
}
顺便说一句,与其使用var deferred = $q.defer();
,不如将该链写成:
one("pass this to one")
.then(two)
.then(three);
相关文章:
- 使用promise和mongoose对文档进行排序
- 测试Angular Service解决错误回调中的promise
- 节点协同与生成器和Promise并行流量控制
- 根据是否解析了 Promise 从函数返回值
- 将一个方法转换为promise:Nodejs
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- js promise没有正确关闭
- Angularjs使用“;这个“;promise内的关键字回调
- AngularJS和promise值在调用本地函数时的效果-未定义
- 在ES6 Promise中,我应该在解决/拒绝之前使用return吗
- 以同步方式获取Javascript Promise的值
- NodeJS和pg promise,捕获PostgreSQL异常
- AngularJS使用http进行promise处理
- Angular promise return"未定义的“;值.NET MVC
- Promise/A+ with chain then() 回调用例
- 减速器中的React/Redux、chain或promise
- Ending a Promise chain
- AngularJS : Seqential Promise chain
- Angularjs: chain promises返回一个promise而不是一个对象
- 在promise-chain中传递引用函数的值