JS ES6 Promise Chaining

后端 未结 5 2065
南笙
南笙 2020-12-10 14:28

I\'m trying to learn how to use promises, but am having trouble comprehending the chaining. I assume that with this code, both promises will run. Then when I call test.then(

5条回答
  •  天涯浪人
    2020-12-10 14:43

    you need to return the other promise(test2) in the first promise (test1) to allow for chaining:

      var test = new Promise(function(resolve, reject){
        resolve('done1');
    });
    
    var test2 = new Promise(function(resolve, reject){
        resolve('done2');
    });
    
    test
    .then(function(data) {
      console.log(data);
      return test2;
    });
    

提交回复
热议问题