如何串联承诺

时间:2017-04-24 01:00:13

标签: javascript promise

我想链接系列承诺,以便3顺序执行。我不想嵌套这些功能。我尝试在其中添加一个解决方案,但它似乎在函数内部的所有内容完成之前解析。

function first(){
  var promise = new Promise(function(resolve, reject){
            console.log('fetchToken 1');
  });
  return promise;
}

function second(item){
  var promise = new Promise(function(resolve, reject){
            console.log('fetchToken 2');
  });
  return promise;
}

function third(item){
  var promise = new Promise(function(resolve, reject){
            console.log('fetchToken 3');
  });
  return promise;
}

  function getTokenGroup() {
    function logTaskError(e) {
        console.error(e);
        throw e;  // reject the Promise returned by then
    }
    var task1 = first();
    var task2 = task1.then(second);
    var task3 = task2.then(third);
    var alltasks = task3.then(null, logTaskError);

    return alltasks;
  }

  getTokenGroup();

3 个答案:

答案 0 :(得分:1)

调用内部函数以解决承诺

function first() {
  var promise = new Promise(function(resolve, reject) {
    console.log('fetchToken 1');
    resolve();
  });
  return promise;
}

function second(item) {
  var promise = new Promise(function(resolve, reject) {
    console.log('fetchToken 2');
    resolve();
  });
  return promise;
}

function third(item) {
  var promise = new Promise(function(resolve, reject) {
    console.log('fetchToken 3');
    resolve();
  });
  return promise;
}

function getTokenGroup() {
  function logTaskError(e) {
    console.error(e);
    throw e; // reject the Promise returned by then
  }
  var task1 = first();
  var task2 = task1.then(second); // why return new promise
  var task3 = task2.then(third);
  var alltasks = task3.then(null, logTaskError);

  return alltasks;
}

getTokenGroup();

或者更好,可以使用then

继续第一个承诺

function first() {
  console.log('fetchToken 1');
}

function second(item) {
  console.log('fetchToken 2');
}

function third(item) {
  console.log('fetchToken 3');
}

function getTokenGroup() {
  function logTaskError(e) {
    console.error(e);
    throw e; // reject the Promise returned by then
  }
  var promise = Promise.resolve(0);  // create a resolved promise
  var task1 = promise.then(first);
  var task2 = task1.then(second);
  var task3 = task2.then(third);
  var alltasks = task3.then(null, logTaskError);

  return alltasks;
}

getTokenGroup();

答案 1 :(得分:0)

您需要解决一个承诺,否则它将不会进一步传播。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

function first(){
   return new Promise(function(resolve, reject){
        console.log('fetchToken 1');
        resolve('your data');
   })
}

答案 2 :(得分:0)

你需要打电话给“解决”:

 function first(){
    var promise = new Promise(function(resolve, reject){
      console.log('fetchToken 1');
      resolve(1); //==> call this one, parameter in here is just a sample
    });
    return promise;
  }

  function second(item){
    var promise = new Promise(function(resolve, reject){
      console.log('fetchToken 2');
    });
    return promise;
  }

  function third(item){
    var promise = new Promise(function(resolve, reject){
      console.log('fetchToken 3');
    });
    return promise;
  }

  function getTokenGroup() {
    function logTaskError(e) {
      console.error(e);
      throw e;  // reject the Promise returned by then
    }
    var task1 = first();
    var task2 = task1.then(second);
    var task3 = task2.then(third);
    var alltasks = task3.then(null, logTaskError);

    return alltasks;
  }

  getTokenGroup();