简体   繁体   English

控制`.then()`回调何时触发

[英]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: 以下是一些代码片段,可帮助说明我的问题:

strategy.js 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 store.js

// 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

store-helpers.js 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();
      }
   });
},

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() in aFn().then(anotherFn) gets called before bFn().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.

相关问题 useEffect 钩子中的回调何时触发? - When callback in useEffect hook fires? #Dart:当jquery向Dart回调方法触发事件时,Div被删除 - #Dart: Divs are removed when jquery fires an event to an Dart callback method 事件侦听器回调在另一个事件侦听器的回调中设置时立即触发。 ReactJS - Event Listener Callback Fires Immediately When Set Inside The Callback of Another Event Listener. ReactJS 在淡出完成之前触发回调 - Callback fires before fadeOut finishes jQuery fadeOut回调永远不会触发 - jQuery fadeOut callback never fires 在迭代完成之前触发回调 - Callback fires before iteration is complete 动画触发回调之前完成 - animate fires callback before complete 当选择列表文本更改时(在onchange事件触发之前)如何触发回调函数? - How to trigger a callback function when select list text changes (before onchange event fires)? 回调不会收到 state 的更新版本,即使它在 state 更改后很好地触发 - Callback doesn't receive the updated version of state even when it fires well after state changes 同构SmartClient是否具有当用户在表单文本项上选择选择列表记录时触发的回调? - Does Isomorphic SmartClient have a callback that fires when a user selects a pick list record on a form text item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM