IamはjQueryを使って4 promises
のオブジェクトを取得しましたが、どのように各約束の解決の価値を得るのですか?私は$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"}] }
の下に持っています
値を解決する方法は?
はい、あなたは正しいです。 1回目のスニペットで、必要な場合に追加するだけです。私はそれが必要かどうかを尋ねていますか? 2番目のスニペットで。私は** catch **ブロックと 'return d.promise()'の中で最後の** then **ブロックと 'd.reject'で' d.resolve(response) 'を使用しています.2番目のスニペットを更新してください –
とにかくネイティブの約束を使用していることを考えれば、単に「Promise.all」に行くのはなぜですか? – Bergi