跳出承诺链或采取不同的路径

时间:2017-04-12 23:34:01

标签: javascript promise

我有一个问题,我可能需要跳出整个承诺链,因为它有一些价值,或者根据一个值采取两条路径。

你最擅长做什么?

这是我想跳出整个链条的第一个场景。我只是想给他们留言。

      DB_WorkIssues.info().then(function (details) {
     if (details.doc_count == 0 && details.update_seq == 0) {
        showMsg("This device has no local data on it and no access to the Server, please come back when you are online.")
     }  jump out here, no need to do the next set.
     else
     return;     Continue on as the values are valid.
      }).then(function () {
     return ajaxCallForJson(URI_LookupTables);
      }).then(function (json) {
     return callBulkDocLoad(DB_LookupTables, json);
      }).then(function () {
     return loadCategoriesDDL();
      }).then(function () {
     return loadEquipmentDDL();
      }).catch(function (err) {
     showMsg("Error in defineDBs: " + err);
      });

在第二种情况下,如果值是一样,我可能想要一条路径,如果值是另一条,我可能想要另一条路径。但我仍然希望连锁店能够实现第一个承诺。像这样:

      DB_WorkIssues.info().then(function (details) {
     if (details.doc_count == 0 && details.update_seq == 0) {

     Take this path.
          return;
          }).then(function () {
         return ajaxCallForJson(URI_LookupTables);
          }).then(function (json) {
         return callBulkDocLoad(DB_LookupTables, json);
          }).catch(function (err) {
         showMsg("Error in defineDBs: " + err);
          });

     }  
     else
     {

     Take this path instead

         return;
          }).then(function () {
         return loadCategoriesDDL();
          }).then(function () {
         return loadEquipmentDDL();
          }).catch(function (err) {
         showMsg("Error in defineDBs: " + err);
          });
    }

感谢。

这是我在查看答案之后的想法,我总是做第二个承诺,在某些情况下只做第一个。

    DB_WorkIssues.info().then(function(details) {

      // promise variable , defined in conditional
      var promise;

    Would I set the promise to some default value, in case the following test fails

      if (details.doc_count == 0 && details.update_seq == 0) {
        // return this promise
        promise = ajaxCallForJson(URI_LookupTables).then(function(json) {
          return callBulkDocLoad(DB_LookupTables, json);
        });

      }
      return promise;

    }).then(function () {
       return loadCategoriesDDL();
    }).then(function () {
       return loadEquipmentDDL();
    }).then(function () {
       return loadLocationsDDL();
    }).catch(function (err) {
       showMsg("Error in defineDBs: " + err);
    });

我是怎么做到的?

感谢。

2 个答案:

答案 0 :(得分:1)

我认为这是一个代表你的目标的骨架。承诺是非常强大的,值得研究。我尝试添加有用的评论,但我建议使用代码并了解正在进行的操作。



// Six named promise-creators. When called with (x), will create a promise
// which waits 200ms and then logs and resolves with (x). 
// These could represent any asynchronous operation.
const p1 = p2 = p3 = p4 = p5 = p6 = 
  (x) => {
    const p = new Promise((resolve, reject) => {
      setTimeout(() => {resolve(x); console.log(x)}, 200)
  });
  return p;
}

// A function which, when called, will execute first promise chain.
const first_steps = () => 
  p1(1)
    .then(result => p2(2))
    .then(result => p3(3))

// A function which, when called, will execute second promise chain.
const second_steps = () => 
  p4(4)
    .then(result => p5(5))
    .then(result => p6(6))

// When true, this prints numbers 1-6.
// When false, only prints numbers 4-6.
if (false) {
 console.log(first_steps().then(second_steps));
} else {
 second_steps();
}




答案 1 :(得分:0)

在我看来,你有额外的then()套,而你的if()会决定返回类似的承诺:

DB_WorkIssues.info().then(function(details) {

  // promise variable , defined in conditional
  var promise;

  if (details.doc_count == 0 && details.update_seq == 0) {
    // return this promise
    promise = ajaxCallForJson(URI_LookupTables).then(function(json) {
      return callBulkDocLoad(DB_LookupTables, json);
    });

  } else {
    // or this promise
    promise = loadCategoriesDDL().then(function() {
      return loadEquipmentDDL();
    });
  }

  promise.catch(function(err) {
    showMsg("Error in defineDBs: " + err);
  });

  return promise;

})