1

The situation is as follows:

function evaluateUser(){
//AJAX call, returning true or false
}

if(evaluateUser()){
//do something
}else{
//do something else
}

AFAIK jqueryAJAX calls create a promise. So I can indeed do something like this:

function evaluateUser(){
//return AJAX Call
}

The problem is that I'm just returning a promise, and not the result of the async operation. However, since I'm new to promises, I wonder if I could do something with resolve/reject?

I haven't seen any resolve/reject syntax inside jquery AJAX calls yet, but since they create promises, resolve/reject must be implemented inside them, aren't they?

And if so, can one somehow use resolve/reject to make a distinction between true/false? The async operation really does just that, in the end. So while I can't really "unwrap" the promise generated by the AJAX call, is there at least a way to distinguish between a promise that was rejected and a promise which was resolved? So that I could somehow make the true/false result inside the asynchronous world visible to the synchronous world?

Phil
  • 157,677
  • 23
  • 242
  • 245
ForeFather321
  • 179
  • 2
  • 9
  • 1
    You will have to call `.then` on `evaluateUser()` *before* entering the `if` statement in order to differentiate a truthy response from a falsey response. – CertainPerformance Aug 30 '18 at 06:58
  • `evaluateUser().then(good => { if (good) { // do something } else { // do something else } });` – DrevanTonder Aug 30 '18 at 07:00
  • 1
    so, basically I have to put ALL the operations following this asynchronous operation into the "callback" zone of the evalueUser() function? I know we are talking promises here, but if I understood promises correctly, at their core they are a cleaner solution than callbacks, but ultimately they work like callbacks (which makes it so hard/impossible to get anything happening inside of them visible to the outside world) – ForeFather321 Aug 30 '18 at 07:01
  • 1
    @ForeFather321 correct – DrevanTonder Aug 30 '18 at 07:02
  • If you don't like callbacks, check out [`async / await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) – Phil Aug 30 '18 at 07:06
  • Im already using async/await, but this doesn't change anything about the fact that I can't get anything OUTSIDE the scope of promises, since async/await only worx with promises being returned. – ForeFather321 Aug 30 '18 at 07:27

0 Answers0