I have an app with two submit buttons. The first initiates an API call via a promise which takes a number of seconds. The second button gathers some user input then processes the output of the API request with their selection.
I'd like to start the request, have the user make their selection, then wait for the initial request to complete when they hit submit:
this.upload.addEventListener('change', (event) => {
....
// initial submit button
API.getHumanFaceLandmarks(data.corrected).then((landmarks) => {
console.log(landmarks);
this.humanFeatures = extractHumanFeatures.beginRender(
landmarks,
data.canvas,
this.EYE_MASK,
this.MOUTH_MASK,
this.EYE_SELECTOR,
this.MOUTH_SELECTOR);
}).catch(error => console.log('error', error));
});
pet.addEventListener('click', (event) => {
// second submit
renderPetswitch.outputFinalImage(
event.target.dataset.name,
this.humanFeatures, // <--- how do i wait for this value to be resolved before executing this function?
this.OUTPUT_CANVAS);
})
My API method looks like this:
getHumanFaceLandmarks: async function(data) {
console.log(data.substring(0, 50) + '...');
const headers = new Headers();
headers.append("Content-Type", "application/json");
const body = JSON.stringify({
"image": data
});
const requestOptions = {
method: 'POST',
headers: headers,
body: body,
redirect: 'follow'
};
const response = await fetch(constants.HUMAN_FACE_LANDMARKS_ENDPOINT, requestOptions);
return response.json();
}
How do I wait for an exisiting promise to complete when the user submits the second step of the process?
I'm trying to streamline the experience and thus would prefer not to do the request in one go.