Promise 只处理成功回调

只处理成功

var A = new Promise(function (resolve, reject) {
  console.log('do sth.');
  setTimeout(() => {
    console.log('~~~~~');
    resolve('ok');
    // reject('error ******');
    console.log('+++++');
  }, 3000); // 延时3秒执行一次结束
});

//    var B = A.then()  //穿透
var B = A.then(value => {
  // 只处理成功
  console.log(1111, value); // 获取A的值一次,再返回给B为undefined
});

setInterval(() => {
  console.log(A);
  console.log(B);
  console.log('~~~~~~~~~~~~~~~~~~~~');
}, 1000);
console.log('===================');

效果

root@ubuntu:~/workspace/es6/babel# node dist/index.js 
Debugger attached.
do sth.
===================
Promise { <pending> }
Promise { <pending> }
~~~~~~~~~~~~~~~~~~~~
Promise { <pending> }
Promise { <pending> }
~~~~~~~~~~~~~~~~~~~~
~~~~~
+++++
1111 ok
Promise { 'ok' }
Promise { undefined }
~~~~~~~~~~~~~~~~~~~~
Promise { 'ok' }
Promise { undefined }
~~~~~~~~~~~~~~~~~~~~
Promise { 'ok' }
Promise { undefined }
~~~~~~~~~~~~~~~~~~~~
^C
posted @ 2022-10-18 11:01  flytoyou  阅读(29)  评论(0编辑  收藏  举报