Promise

var log = function(value){ console.log( value) ;}

Promise.resolve(1)
      .then(it=>  log(it) )                                                      // 输出1
      .then(it=> { log(it);  return Promise.resolve(2); } )      //输出 undefined
      .then(it=> { log(it); return 3 ;})                                    //输出2
      .then(it=> { log(it) ; return Promise.reject(4)  })         //输出3
      .then(it=> log("OK") )                                                // 不会输出 OK
      .catch(it=> { log(it) ; return Promise.resolve(5) ;})     //输出4
      .then(it=>  log(it) )                                                      //输出 5

结论

then 里的返回值, 会传递给下一个 then 的入参
then 如果返回 Promise.resolve , 则下一个 then 的入参是 Promise.resolve 的值 .
then 如果返回 Promise.reject , 则执行一下 catch .
then 的执行是 串行执行.

posted @ 2018-10-29 15:47  NewSea  阅读(198)  评论(0编辑  收藏  举报