I am using React to write code for my Apps script add-on. I want handleCancel to be executed on click of the Cancel button. It executes someAsyncMethod and then closes the modal using google.script.host.close().
export const SampleCmp = () => {
const handleCancel = async () => {
await someAsyncMethod();
google.script.host.close();
};
return (
<button
onClick={handleCancel}
>
Cancel
</button>
);
};
const someAsyncMethod() = async() : Promise<SomeObject> => {
};
someAsyncMethod takes too long (~5s) to execute and I want the modal to close immediately. If I remove the await keyword then someAsyncMethod does not complete with the resolved value or gets suspended, which I suspect happens because of the google.script.host.close. Is there a way the someAsyncMethod can continue in the background but we are able to close the modal immediately? Thanks.
AFAIK it's not possible to keep running client-side code (JavaScript) when the host (in this case the modal) is closed. You need to consider a different approach, i.e. instead of using a modal you could use a sidebar, keeping it opened until someAsyncMethod finish.
Maybe the use a Google Apps Script add-on as a whole solution is a bad idea and you will have to rethink the solution implementation i.e. web app + add-on or as web-browser extension + add-on.