简体   繁体   English

我的承诺在我认为应该解决之前就已解决

[英]My promise is resolving before I think it should

I am trying to use q promises to handle some long-running ajax requests and subsequent data processing. 我正在尝试使用q promises处理一些长期运行的ajax请求和后续的数据处理。 I have a case where I am trying to create some promises that should be executed in sequence, but they seem to be resolving before I want them to and I'm not sure why. 我遇到的情况是我试图创建应按顺序执行的promise,但是在我希望它们执行之前它们似乎已经解决了,我不确定为什么。

The main sequence is to first try some "main" work which should be done asynchronously, then do some "follow-up" work which itself comprises a set of several async requests. 主要顺序是首先尝试应异步完成的一些“主要”工作,然后再进行一些“后续”工作,这些工作本身包括一组几个异步请求。 Here's the main body from my fiddle which demos the problem: 这是我的小提琴中演示问题的主体:

var to_do = ["one", "two", "three", "four"];
var result = Q();
to_do.forEach(function (item) {
  result = result
    .then(dowork.bind(null, item))
    .fail(handleError)
    .then(dofollowup.bind(null, item));
});

var dowork = function (value) {
  var deferred = Q.defer();
  log("About to do main work for " + value);
  setTimeout(function () {
    if (value === 'two') {
      // represent a possible error in this work, but we still need to do follow-up work
      deferred.reject("An error occurred doing work for number two, but we should continue");
    } else {
      log("Done main work for " + value);
      deferred.resolve(value);    
    }
  }, 1000);
  return deferred.promise;
}

var dofollowup = function (value) {
  log("Doing follow-up work for " + value);
  var to_do = Q();
  for (var i=0; i<5; i++) {
    to_do = to_do.then(function () {
      var deferred = Q.defer();
      setTimeout(function () {
        log("Doing delayed work for " + value);
        deferred.resolve();
      }, 100);
    });
  }
  return to_do;
}

My immediate problem is that the "main" work for the next item is being started before the "follow-up" work for an item is finished. 我的直接问题是,在完成项目的“跟进”工作之前,开始进行下一项的“主要”工作。 I am guessing I'm just not dealing with the promises correctly and accidentally resolving it too soon, but I don't see how at the moment. 我猜我只是没有正确地兑现承诺,而意外地过早地兑现了承诺,但目前还不知道如何实现。

I created a fiddle that simulates my problem. 我创建了一个模拟我的问题的小提琴。 In it, I see the work for the "two" work item starting before the follow-up for "one" is complete. 在其中,我看到“两个”工作项的工作在“一个”的后续工作完成之前开始。 How can I insure that I complete each item's follow-up before starting on the main work for the next? 在开始下一项的主要工作之前,如何确保完成每一项的后续工作?

https://jsfiddle.net/cfarmerga/cp323djx/1/ https://jsfiddle.net/cfarmerga/cp323djx/1/

在此处输入图片说明

Modifying your dofollowup to return the deferred seems to fix this. 修改您的dofollowup以返回延迟的值似乎可以解决此问题。

var dofollowup = function (value) {

  log("Doing follow-up work for " + value);

  var to_do = Q();
  for (var i=0; i<5; i++) {
    to_do = to_do.then(function () {
      var deferred = Q.defer();
      setTimeout(function () {
        log("Doing delayed work for " + value);
        deferred.resolve();
      }, 100);
      return deferred.promise; //you forgot this..
    });
  }
  return to_do;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM