Call the server from the page with async/await
A small helper turns google.script.run into a promise, with errors shown instead of lost.
File js/app.html
<script>
/** server('getItems', 'arg') → a promise of what getItems('arg') returns. */
function server(name, ...args) {
return new Promise((resolve, reject) => {
google.script.run.withSuccessHandler(resolve).withFailureHandler(reject)[name](...args);
});
}
async function load() {
const box = document.getElementById('app');
box.textContent = 'Loading…';
try {
const items = await server('getItems');
box.textContent = items.length ? items.map(i => i.Name).join(', ') : 'Nothing yet.';
} catch (err) {
box.textContent = 'Could not load: ' + err.message;
}
}
load();
</script>
Good to know
- Without withFailureHandler a server error disappears silently and the page keeps waiting.
- Arguments and results must be plain values: text, numbers, true/false, arrays and objects of those (no Date).
Let the AI put it in your appGAS AI Builder knows every recipe here. Describe your app and it uses this code where it fits, in a Google Apps Script project in your own Drive.
Use it in a projectMore in Web app
Who is using the app, and their roleReads the visitor's email and looks up their role in a Users tab; server functions check it before doing anything.doGet with pages split into filesindex.html pulls in css/app.html and js/app.html with include(); works on phones and inside an iframe.Return data the page can readTurns dates into text and drops values the page can't receive, so a result never arrives as null.Upload a file from the page to DriveThe page reads the chosen file as base64 and sends it; the server saves it in a folder and returns its link.