簡體   English   中英

控制`.then()`回調何時觸發

[英]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)

以下是一些代碼片段,可幫助說明我的問題:

strategy.js

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);
}

},

store.js

//只是一個包裝了同步get get的代理函數:function(key,options){return this.getItem.apply(this,arguments); },

getItem: StoreHelpers.proxyGetItem

store-helpers.js

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM