我有个关于连锁承诺的问题。
这是我的密码:
var removeProducts = function(id) {
anotherService.removeProducts(id); // this will return a promise.
}
var addProduct = function(id) {
myService.addProduct({id: id})
}
$scope.pickProduct = function() {
myService.updateId({'id':123}).$promise.then(function(items) {
items.categories.filter(function(category) {
if (category.type === 'NEW') {
removeProducts(items.id);
}
})
//this is the part I don't know how to proceed. I need to make sure
//the product is removed before first and update ID second so I can add
//another product.
addProduct(item.id);
})
}基本上,每次添加或删除产品时,我都需要从myService调用myService方法。这些步骤如下:
Update ID
Remove product if there is a type 'New'
Update ID
Add product我该怎么改变这个?谢谢你的帮助!
发布于 2016-02-24 20:01:00
基本上你可以像这样连锁承诺:
myService.pickProduct(product)
.then(_update)
.then(_remove)
.then(_add)
.catch(..);
function _update(product) {
return product.id;
}
function _remove(id) {
return new Date();
}
function _add(date) {
}then的返回值将是下一个“链”承诺then的输入。在我的例子中,服务函数pickProduct必须返回持有该产品的承诺,因为我期望它作为_update中的输入等等。
发布于 2016-02-24 19:34:57
调用updateID函数,然后从removeProduct返回。如下所示:
$scope.pickProduct = function() {
myService.updateId({
'id': 123
}).$promise.then(function(items) {
items.categories.filter(function(category) {
if (category.type === 'NEW') {
removeProducts(items.id).then(function() {
//call updateId .then(function(){
// call add product
}
};
}
})
})
}https://stackoverflow.com/questions/35611245
复制相似问题