Iam使用jQuery获取对象4 promises
作为回应,现在如何获得每个promise的resolves值?在角度我可以使用$q.all()
和.then()
它将解决所有值;但在jQuery中不起作用。下面是代码片段如何解决jQuery中的承诺链?
var _service = function() {};
var ec2;
_service.prototype = {
init: function(formdata, cb) {
console.info('::init::', arguments[0]);
ec2 = new AWS.EC2(ec2_config);
return ec2.promise();
},
loop: function(param) {
console.info('createAdditionalResources params:', arguments);
return $.when({
'fun1': this.fun1(param),
'fun2': this.fun2(param),
'fun3': this.fun3(param),
'fun4': this.fun4(param)
}).done(function(response) {
console.log('Done All');
return response;
});
},
fun1: function (param) {
return something.promise(); // return promise
},
....
};
window.obj = new _service();
这里我调用这个函数在我的剧本
function foo() {
var d = $.Deferred();
obj.init()
.then(function(res) {
return obj.loop();
})
.then(function(res) {
console.log(res); // this is promise chain
d.resolve(res);
})
.catch(function(err) {
d.reject(err);
});
return d.promise();
};
这里资源带有此{fun1: Promise, fun2: Promise, fun3:Promise, fun4:Promise }
和FUN1低于价值
fun1: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: { InternetGateway: { Attachments:[{InternetGatewayId:"igw-6253970b"}] }
现在如何解决价值?
是的,你是对的;在第一个片段中,我只是在需要的情况下添加?我在问我们是否需要它?在第二个片段中;我在上一个**中使用了'd.resolve(response)',然后在** catch **块中使用了**块和'd.reject',并且使用了'return d.promise()'。我更新了第二个片段 –
鉴于(至少在'fun1'中)无论如何你都使用本地promise,为什么不简单地去'Promise.all' ?! – Bergi