javascript - How to wait for a function to finish its execution in angular 2.? -
below code, want login() , authenticated() functions wait getprofile() function finish execution. tried several ways promise etc. couldn't implement it. please suggest me solution.
import { injectable } '@angular/core'; import { tokennotexpired } 'angular2-jwt'; import { myconfig } './auth.config'; // avoid name not found warnings declare var auth0lock: any; @injectable() export class auth { // configure auth0 lock = new auth0lock(myconfig.clientid, myconfig.domain, { additionalsignupfields: [{ name: "address", // required placeholder: "enter address", // required icon: "https://example.com/address_icon.png", // optional validator: function(value) { // optional // accept addresses more 10 chars return value.length > 10; } }] }); //store profile object in auth class userprofile: any; constructor() { this.getprofile(); //i want here function finish work } getprofile() { // set userprofile attribute if saved profile this.userprofile = json.parse(localstorage.getitem('profile')); // add callback lock `authenticated` event this.lock.on("authenticated", (authresult) => { localstorage.setitem('id_token', authresult.idtoken); // fetch profile information this.lock.getprofile(authresult.idtoken, (error, profile) => { if (error) { // handle error alert(error); return; } profile.user_metadata = profile.user_metadata || {}; localstorage.setitem('profile', json.stringify(profile)); this.userprofile = profile; }); }); }; public login() { this.lock.show(); this.getprofile(); //i want here function finish work }; public authenticated() { this.getprofile(); //i want here function finish work return tokennotexpired(); }; public logout() { // remove token , profile localstorage localstorage.removeitem('id_token'); localstorage.removeitem('profile'); this.userprofile = undefined; }; }
like saw in comments, have use promise
or observable
achieve this, since behaviour pretty simple, should use promise
because observable
have lot of features don't need in case.
here promise
version of service:
import { injectable } '@angular/core'; import { tokennotexpired } 'angular2-jwt'; import { myconfig } './auth.config'; // avoid name not found warnings declare var auth0lock: any; @injectable() export class auth { // configure auth0 lock = new auth0lock(myconfig.clientid, myconfig.domain, { additionalsignupfields: [{ name: "address", // required placeholder: "enter address", // required icon: "https://example.com/address_icon.png", // optional validator: function(value) { // optional // accept addresses more 10 chars return value.length > 10; } }] }); //store profile object in auth class userprofile: any; constructor() { this.getprofile(); //i want here function finish work } getprofile():promise<void> { return new promise<void>(resolve => { // set userprofile attribute if saved profile this.userprofile = json.parse(localstorage.getitem('profile')); // add callback lock `authenticated` event this.lock.on("authenticated", (authresult) => { localstorage.setitem('id_token', authresult.idtoken); // fetch profile information this.lock.getprofile(authresult.idtoken, (error, profile) => { if (error) { // handle error alert(error); return; } profile.user_metadata = profile.user_metadata || {}; localstorage.setitem('profile', json.stringify(profile)); this.userprofile = profile; resolve() }); }); }) }; public login(): promise<void>{ this.lock.show(); return this.getprofile(); //i want here function finish work }; public authenticated():void{ this.getprofile().then( () => { return tokennotexpired(); }); }; public logout():void { // remove token , profile localstorage localstorage.removeitem('id_token'); localstorage.removeitem('profile'); this.userprofile = undefined; }; }
more on promise
here
Comments
Post a Comment