-2

I have an Angular 4 login Application. I want login synchronous operation but asynchronous ? Can you help me?

data.service.ts:

login(userName: string,pwd: string): Promise<User>  {
    const url =`loginLdap/${userName}&${pwd}`;  
    return this.http.get(url).toPromise()
    .then(response => response.json() as User)
    .catch(this.handleError);
}

user.service.ts :

public authenticate( userName: string, pwd: string): Observable<User>{
    this.dataService.login(email, password).then(items => this.items = items);
    console.log(this.items);

    if (this.items.success) {
        this._authenticated = true;
        ...
        return Observable.of(User);
    }else{ 
        return Observable.throw(new Error("System Error"));
    }

}
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Altarian
  • 21
  • 2
  • When you say you want your login operation to be sync, what exactly are you attempting to accomplish? What problems are you seeing with async that causes you to want this? To me that says you want the rest of your JSEE to halt during this process, however I'm guessing that's not really what you want. – iHazCode Feb 25 '18 at 19:21
  • in the above code first goes into if-else block then returns to the http response. – Altarian Feb 25 '18 at 19:43

1 Answers1

0

Please take a look at the Rxjs forkJoin operator. It will allow you to chain multiple Observables then take action after all of them have emit. Inside of the forjoin body add your if/else logic. This would be an elegant way to accomplish what you are looking for.

If you have issues getting this setup, create a plunkr and we can work through it together.

If you don't have issues getting this working, please post the resulting file for others to view when you've gotten it working as desired.

iHazCode
  • 622
  • 4
  • 15