Javascript Promise可以拒绝返回值吗?

时间:2015-11-04 20:22:37

标签: javascript

我正在使用Promises编写一段代码,我想要拒绝Promise并获得一些返回值。基本上在某些情况下盲目拒绝承诺是不够的。我必须根据客户端代码(即Promise catch语句逻辑决定返回)做一些额外的工作。

现在看来拒绝不会返回任何值。

有人可以提出可行的方法吗?有可能吗?

这是我希望能够做到的......更新,因为我可能引起了一些混淆......

function executeSomeLogic() {
  return new Promise((resolve,reject) => {
    var err = doSomeWork();
    if (!err) {
       resolve("Finished without error");
    } else {
      // I cannot get this to work. reject does not return a value.
      // ret is always undefined after this next line
      let ret = reject("Return true if you want to roll back"); 
      if (ret) {
        doSomeInternalCleanup();
      }
    }
  });
}

// this runs in completely different place/module/file and simply invokes executeSomeLogic function. Returning anything from catch does not do anything...

executeSomeLogic().then(result => { 
  console.log("all good");
}).catch(error => { 
  // Here, is client code I want to tell the calling code that I want 
  // to clean up ONLY if error message match what I am looking for.
  // Problem is returning anything from catch is not doing anything to 
  // the code that called reject...
  console.log("no good");
  if (error == "Return true if you want to roll back"){
    return true;
  } else {
    return false;
  }
});

2 个答案:

答案 0 :(得分:0)

您正在 ret 变量中保存Promise对象,而不是值...

尝试:class TimelineViewController: UIViewController, TimelineComponentTarget, UITabBarControllerDelegate {

答案 1 :(得分:-1)

一个测试案例可能是:

let ret = Promise.reject( 'my rejected Value' );
ret.then(
  val => {console.log('fulfilled val :' , val);return val;}  ,
  val => {console.log('rejected val :' , val);return val;}
).then(
  rep => console.log('response : ' , rep)
)

为了更好地理解,我在代码中添加了一些console.log (另见评论)

function executeSomeLogic() {
  return new Promise((resolve,reject) => {
    var err = doSomeWork();
    
    // this part below  is never reached
    // never executed because doSomeWork() don't exists
    
    console.log('we pass here');
    
    if (!err) {
       resolve("Finished without error");
    } else {
      var ret = reject("Return false if you want to roll back");
      if (ret) {
        console.log(' i need to doSomeInternalCleanup()');
        doSomeInternalCleanup();
      }
    }
  });
};




executeSomeLogic().then(result => { 

  // We Go directly to the catch  
  
  console.log("all good");

}).catch(error => { 
  
  console.log("no good");
  console.log(error); // ---> ReferenceError: doSomeWork is not defined
  
  if (error == "Return false if you want to roll back"){
    console.log('true');
    return true;

  } else {
  
    console.log('false'); // ----> this is printed
    return false;
  }
});

让你的行为测试这段代码:

//console.clear();
function doSomeWork() { 
  //return Boolean(Math.floor( Math.random() * 2));
  return true;
};


function executeSomeLogic() {
  return new Promise((resolve,reject) => {
    var err = doSomeWork();
    
    
    console.log('we pass here');
    
    if (!err) {
       resolve("Finished without error");
    } else {
      var ret = reject("Return false if you want to roll back");
      
      console.log('we have rejected' , ret);
      if (ret) {
        // never reached because reject don't return something !
        console.log('because i have ret i need to doSomeInternalCleanup()' , ret);
        //doSomeInternalCleanup();
      }
      
      console.log('but i can doSomeInternalCleanup() even without ret' ,ret);
      
    }
  });
  
   
  
};




executeSomeLogic().then(result => { 

  // We Go directly to the catch  
  // but if you had returned something valid like 'cleanupDone'
  // the value of result would be 'cleanupDone'
  
  console.log("all good");

}).catch(error => { 
  
  console.log("no good");
  console.log(error); // ---> "Return false if you want to roll back"
  
  if (error == "Return false if you want to roll back"){
    console.log('true'); // ----> this is printed
    return true;

  } else {
  
    console.log('false'); 
    return false;
  }
});