[英]Control when a `.then()` callback fires
我有很长的承诺承诺会贯穿我的代码模块。 我事先不知道我会完成多少个承诺,也没有范围从任何一个承诺到其他任何承诺(这意味着我无法执行Promise.join()
)。
我的问题是我在该链上的多个promise上附加了then()
回调。 如何控制最后一个被解雇?
更新 :这是一个简化的示例:
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);
我的问题是.then()
在aFn().then(anotherFn)
被称为前bFn().then(someFn)
以下是一些代码片段,可帮助说明我的问题:
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);
}
},
//只是一个包装了同步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();
}
});
},
在应用程序的非常不同的部分,我还有:
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);
}
我希望上面的.resolve
.then()
最后触发,但是当.resolve
中的.resolve被触发时,最后一个.then .then()
回调将被调用。
我的问题是
.then()
在aFn().then(anotherFn)
被称为前bFn().then(someFn)
不,不是。 在编写示例时,该表达式等效于
bFn().then(someFn).then(anotherFn)
-尽管someFn
.then()
方法确实在someFn
之前被调用,而anotherFn
回调没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.