[英]Control when a `.then()` callback fires
I have a long chain of promises that wind through a module of my code. 我有很长的承诺承诺会贯穿我的代码模块。 I don't know beforehand how many promises I'll wind through, nor do I have scope from any one promise to any other other promise (meaning I can't do a Promise.join()
). 我事先不知道我会完成多少个承诺,也没有范围从任何一个承诺到其他任何承诺(这意味着我无法执行Promise.join()
)。
My problem is that I have then()
callbacks attached to multiple promises on this chain. 我的问题是我在该链上的多个promise上附加了then()
回调。 How can I control which one is fired last? 如何控制最后一个被解雇?
UPDATE : Here's a simplified example: 更新 :这是一个简化的示例:
var aFn = function () {
return bFn().then(someFn);
};
var bFn = function () {
return new Promise(function (resolve, reject) {
if (a) return resolve();
else return reject();
});
};
aFn().then(anotherFn);
My problem is that the .then()
in aFn().then(anotherFn)
gets called before bFn().then(someFn)
. 我的问题是.then()
在aFn().then(anotherFn)
被称为前bFn().then(someFn)
Here are a few code snippets that helps illustrate my issue: 以下是一些代码片段,可帮助说明我的问题:
execute: function (model, options) {
options.url = this.policy.getUrl(model, this.method, options);
options.collection = this.policy.getCollection(model, options);
options.model = model;
// This is a Promise that eventually calls get()
return this.sync(model, options);
},
get: function (key, options) {
var updateCollection = this._getUpdateCollection(options);
var getFromCache = _.bind(this.store.get, this.store, key, options);
if (updateCollection) {
// updateCollection received a promise from store-helpers.js:proxyGetItem()
return updateCollection().then(
function (collection) {
var modelResponse = collection.policy.findSameModel(collection.raw, options.model);
return modelResponse ? modelResponse : getFromCache();
},
getFromCache
);
} else {
return getFromCache();
}
},
_getUpdateCollection: function (options) {
var collection = options && options.collection;
var collectionControl = collection && collection.sync && collection.sync.hoardControl;
if (collection && collectionControl) {
var collectionKey = collectionControl.policy.getKey(collection, options);
return _.bind(function () {
// store.get() passes the returned promise of store-helpers.js:proxyGetItem()
return this.store.get(collectionKey, options).then(function (rawCollection) {
return {
control: collectionControl,
policy: collectionControl.policy,
key: collectionKey,
raw: rawCollection
};
});
}, this);
}
}, },
// Just a proxy function that wraps a synchronous get get: function (key, options) { return this.getItem.apply(this, arguments); //只是一个包装了同步get get的代理函数:function(key,options){return this.getItem.apply(this,arguments); }, },
getItem: StoreHelpers.proxyGetItem
proxyGetItem: function (key, options) {
return Hoard.Promise.resolve()
.then(_.bind(function () {
return this.backend.getItem(key);
}, this))
.then(function (raw) {
var storedValue = JSON.parse(raw);
if (storedValue !== null) {
return storedValue;
} else {
return Hoard.Promise.reject();
}
});
},
In a very different part of the app I also have: 在应用程序的非常不同的部分,我还有:
var originalExecute = Hoard.Strategy.prototype.execute;
Hoard.Strategy.prototype.execute = function (model, options) {
options.originalOptions = _.clone(options);
if (options.saveToCacheFirst)
return originalExecute.call(this, model, options)
.then(_.result(options.originalOptions, 'success'), _.result(options.originalOptions, 'error'));
return originalExecute.call(this, model, options);
}
I would like for the .then()
above to fire last, however when .resolve
in store-helpers.js is fired, this last .then()
callback is invoked. 我希望上面的.resolve
.then()
最后触发,但是当.resolve
中的.resolve被触发时,最后一个.then .then()
回调将被调用。
My problem is that the
.then()
inaFn().then(anotherFn)
gets called beforebFn().then(someFn)
我的问题是.then()
在aFn().then(anotherFn)
被称为前bFn().then(someFn)
No it doesn't. 不,不是。 As you've written the example, that expression is equivalent to 在编写示例时,该表达式等效于
bFn().then(someFn).then(anotherFn)
- and while the .then()
method does get called before someFn
, the anotherFn
callback does not. -尽管someFn
.then()
方法确实在someFn
之前被调用,而anotherFn
回调没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.