I have a function that retrieves location information and returns a promise. I use mobx to manage the store, updating the this.locationStoreProp
and this.hotel.subtext
properties.
public fetchPropertyLocation(some_input_params): Promise<any> {
return this.apiSource
.fetchPropertyLocationData(input)
.then(({ data }) => {
runInAction('update from response', () => {
if (data) {
this.locationStoreProp = data.location;
this.hotel.subtext = data.location.score;
}
});
})
.catch(error => {
runInAction('error in location', () => {
//log event
});
return {
error
};
});
}
The issue arises because the this.hotel
property depends on another fetch operation below, which must complete before the above promise can set the subtext
in this.hotel
. However, the fetchHotelInfo
function is used inside another fetch
(below it).
public fetchHotelInfo(input_params): Promise<any> {
return this.hotelInfoSource
.fetch(input)
.then(data => {
runInAction('update hotel from response', () => {
this.hotel = data;
});
return data;
})
.catch(error => {
runInAction('recovering from hotel info call failure', () => {
//log event
});
return {error};
});
}
public fetch(input_params) {
const { hotelId, searchCriteria } = this;
const infoPromise = this.fetchHotelInfo(input_params);
const someOtherPromise = this.someOtherOperation(input_params);
return Promise.all([infoPromise, someOtherPromise])
.then(
() =>
new Promise(resolve => {
runInAction('updating', () => {
this.isLoading = false;
resolve();
});
})
)
.catch(
() =>
new Promise(resolve => {
runInAction('updating hotel with failure', () => {
this.isFailure = true;
resolve();
});
})
);
}
Finally, when awaiting them, I want the promise of the fetch
function to resolve first with its fetchHotelInfo
promise. However, currently, my location function's promise resolves first, causing the property this.hotel
to be undefined.
public async fetch(options: FetchOptions): Promise<any> {
await fetchClassObj.fetch(params);
await fetchClassObj.fetchPropertyLocation(params);
}
What could be going wrong here? Thanks. Also, please disregard any syntax errors for now.