I'm in the process of getting my feet wet with SPFx Application Customizers and have a question regarding the use of promises in TS.
Before my onRender() method is called inside of onInit(), I need to call two functions using PnP JS:
- The first retrieves the web title
- The second fetches some user-profile metadata
How can I ensure these two functions complete before the onRender() logic is executed?
public onInit(): Promise<void> {
Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);
pnp.sp.web.select("Title").get().then(w => {
this._webTitle = w.Title;
});
pnp.sp.profiles.myProperties.get().then(function (result) {
var userProperties = result.UserProfileProperties;
console.log(userProperties);
userProperties.forEach(function (property) {
if (property.Key == "PreferredName") {
this._userDisplayName = property.Value;
console.log('user display name: ' + this._userDisplayName);
}
});
}).catch(function (error) {
console.log("Error: " + error);
});
return Promise.resolve<void>();
}
Currently, I have a race-condition issue where my application customizer is rendered before getting a response in my onInit() functions.