promise 特点总结

(1)promise的resolve不支持第二个参数的传递(不是大问题)

process.stdin.setEncoding('utf8');



let promise = new Promise(function(resolve, reject) {

  process.stdin.on('readable', () => {
    var chunk = process.stdin.read();
    if (chunk !== null) {
      if(chunk==1)
      {
        resolve({test1:'succ1',test2:'succ2'},{"resolve":resolve,"reject":reject});
      }
      else{
        reject({test1:'error1',test2:'error2'},{"resolve":resolve,"reject":reject});
      }
      // process.stdout.write(`data: ${chunk}`);
    }
  });



});

promise.then(function(value1,status) {
  console.log("succ第一次传递then的值是");
  console.log(value1);
  if(1)
  {
    console.log("第一次then(succ)里的status是");
    console.log(status);
    // 当reject,resolve作为第二个参数传递时会报出status==undefined的错误
  }



},function(value1,status){
  console.log("err第一次传递then的值是");
  console.log(value1);
});
promise.then(function(value1) {
  console.log("succ第2次传递then的值是");
  console.log(value1);

},function(value1) {
  console.log("err第2次传递then的值是");
  console.log(value1);

});

(2)promise 不支持第一个then之后的分支选择(是个大问题)

process.stdin.setEncoding('utf8');



let promise = new Promise(function(resolve, reject) {

  process.stdin.on('readable', () => {
    var chunk = process.stdin.read();
    if (chunk !== null) {
      if(chunk==1)
      {
        resolve({test1:'succ1',test2:'succ2',"resolve":resolve,"reject":reject});
      }
      else{
        reject({test1:'error1',test2:'error2',"resolve":resolve,"reject":reject});
      }
      // process.stdout.write(`data: ${chunk}`);
    }
  });



});

promise.then(function(value1) {
  console.log("succ第一次传递then的值是");
  console.log(value1);
  if(1)
  {


    // 测试作为一个参数传递的情况
     console.log("第一次传递then的reject是");
     console.log(value1.reject);
     value1.reject("then2");
     //**分支选择错误**
     //**走的仍然是"succ第2次传递then的值是"这个提示**
  }



},function(value1,status){
  console.log("err第一次传递then的值是");
  console.log(value1);
});
promise.then(function(value1) {
  console.log("succ第2次传递then的值是");
  console.log(value1);

},function(value1) {
  console.log("err第2次传递then的值是");
  console.log(value1);

});



(3) promise不支持return式的参数变化机制(小问题)

process.stdin.setEncoding('utf8');



let promise = new Promise(function(resolve, reject) {

  process.stdin.on('readable', () => {
    var chunk = process.stdin.read();
    if (chunk !== null) {
      if(chunk==1)
      {
        resolve({test1:'succ1',test2:'succ2'},{"resolve":resolve,"reject":reject});
      }
      else{
        reject({test1:'error1',test2:'error2'},{"resolve":resolve,"reject":reject});
      }

    }
  });



});

promise.then(function(value1,status) {
  console.log("succ第一次传递then的值是");
  console.log(value1);
  if(1)
  {

      // value1.test1="succthen";
      // delete value1.test2;
      // 直接在原有的resolve对象上修改生效
      // return {"test1":"succthen"};
      // 不支持返回新值的状态改变机制
  }



},function(value1,status){
  console.log("err第一次传递then的值是");
  console.log(value1);
});
promise.then(function(value1) {
  console.log("succ第2次传递then的值是");
  console.log(value1);

},function(value1) {
  console.log("err第2次传递then的值是");
  console.log(value1);

});

总结:
promise实用场景
promise 特点总结_第1张图片

  • 一次分支决定一生
  • 可以再向下传递中改变状态量

你可能感兴趣的:(nodejs入门)