5

I want to wait within a method till a promise return.

public loginOffline(username: string, password: string) {
    return this.database.getItem('currentUser', 'login').then(data => {
        this.userLogin = data;
        console.log(username + ' ' + this.userLogin.username + ' ' + password + ' ' + this.userLogin.password);
        if (username === this.userLogin.username && password === this.userLogin.password) {
            return true;
        } else {
            return false;
        }
    }).catch(err => {
        return false;
    });

}

/********* call the method and descide what to do **************/
if (loginOffline('myname', 'mypassword'){
    // Do something.....
} else {
    // Do something else .....
}
......

this doesn't work. The method to call this loginOffline method just want to know if the login in was successful. I tried many things but nothing worked out.

Can anybody help. Thanks a lot Cheers

Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
M. Fish
  • 266
  • 3
  • 4
  • 17

4 Answers4

10

You just have to chain the promise with another then. Call this way:

loginOffline('myname', 'mypassword').then(result =>{
   if(result){
     //do something
   }else{
     //do something else
   }
})

More on promise chaining

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
7

You can use the Promise object like follows:

public loginOffline(username: string, password: string) {
  return this.database.getItem('currentUser', 'login').then(data => {
      this.userLogin = data;
      console.log(username + ' ' + this.userLogin.username + ' ' + password + ' ' + this.userLogin.password );
      if (username === this.userLogin.username && password === this.userLogin.password) {
        return true;
      } else {
        return false;
      }
    }).catch(err => {
      return false;
    });   
}

loginOffline('myname', 'mypassword').then((result) => {
    if(result) {
      // true was returned
    } else {
      // false was returned
    }
 })
Dhyey
  • 4,275
  • 3
  • 26
  • 33
  • 1
    [this is an antipattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – Suraj Rao Jun 02 '17 at 07:27
  • @suraj I took a look at the link you gave me and edited the answer. Can you please take a look and let me know if I understood it correctly. Please feel free to edit and correct me if I am wrong. – Dhyey Jun 02 '17 at 07:43
  • 1
    you wont have `resolve(bool)` since it is not a parameter anymore.. just `return boolean_value`.. – Suraj Rao Jun 02 '17 at 08:04
  • 1
    Inside the callbacks where you return `resolve(true)` or `resolve(false)` you should just be returning `true` or `false` (as in the original code in the question). There is no `resolve` function in scope where you try to use it, but more importantly the handler callbacks for a Promise will automatically wrap any returned value up as a Promise without you having to do anything special. – Duncan Jun 02 '17 at 08:17
1

The answer from @suraj is the correct one, simply use another .then() around the returned result, but an alternative is to use the await/async keywords to rewrite the code.

 public async loginOffline(username: string, password: string) {
    try {
      this.userLogin = await this.database.getItem('currentUser', 'login');
    } catch(e) {
      return false;
    }
    console.log(username + ' ' + this.userLogin.username + ' ' + password + ' ' + this.userLogin.password );
    return (
      username === this.userLogin.username &&
      password === this.userLogin.password);
  }

// This code must also be inside a function marked as 'async':
/********* call the method and descide what to do **************/
if(await loginOffline('myname', 'mypassword'){
// Do something.....
} else {
// Do something else .....
}
......

Note that every async function will automatically convert anything it returns into a Promise, so you have to use async all the way up the call tree. Or, if for some reason you don't want to make the caller async you can just use then on the result as before:

// If the caller cannot be marked `async`:
/********* call the method and descide what to do **************/
loginOffline('myname', 'mypassword').then(result => {
  if (result) {
  // Do something.....
  } else {
  // Do something else .....
  }
});
......
Duncan
  • 92,073
  • 11
  • 122
  • 156
0

you can find the solution here:

function GetUserPemission(){
  return new Promise((resolve,reject)=>{
   getdata().then((data)=>{
      if(data){
            resolve(true);
      }else{
        resolve(false);
      }
    }).catch((error)=>{
            resolve(false);
    })
  })
}

function getdata(){
return new Promise((resolve,reject)=>{ setTimeout(()=>{resolve("Data")},100)})
}

GetUserPemission().then((data)=>{
        alert(data);
});
jyothi
  • 1
  • 1
  • 1
    As I mentioned in the other comment.. wrapping with new promise each time is [an anti pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – Suraj Rao Jun 02 '17 at 08:08