When client scripts rely on multiple GlideAjax calls, callback nesting can quickly become hard to read and maintain. A small Promise wrapper gives you cleaner flow, easier composition, and centralized error handling.
Promise wrapper for GlideAjax This helper:
accepts Script Include name, method name, and params object resolves with answer rejects when the response is malformed or parsing fails /** * Promisified version of GlideAjax * @param {string} scriptInclude - Name of client-callable Script Include * @param {string} methodName - Method name inside Script Include * @param {Object} params - Params object, for example: { sysparm_user_id: '...' } * @returns {Promise<string>} Resolves with "answer" returned by the Script Include */ function uGlideAjaxAsync(scriptInclude, methodName, params) { return new Promise(function(resolve, reject) { try { var ga = new GlideAjax(scriptInclude); ga.addParam('sysparm_name', methodName); for (var key in (params || {})) { if (Object.prototype.hasOwnProperty.call(params, key)) { ga.addParam(key, params[key]); } } ga.getXML(function(response) { try { var xml = response && response.responseXML; var root = xml && xml.documentElement; if (!root) { reject(new Error('GlideAjax response did not contain XML documentElement')); return; } resolve(root.getAttribute('answer')); } catch (parseError) { reject(parseError); } }); } catch (setupError) { reject(setupError); } }); } Usage example uGlideAjaxAsync('UserUtils', 'getManagerSysId', { sysparm_user_id: g_form.getValue('requested_for') }) .then(function(managerSysId) { if (managerSysId) { g_form.setValue('manager', managerSysId); } }) .catch(function(error) { console.error('Failed to fetch manager via GlideAjax', error); g_form.addErrorMessage('Unable to get manager information right now.'); }); Why this is useful Cleaner orchestration for sequential async calls. Better error propagation than deeply nested callbacks. Reusable helper you can keep in one UI Script. If your instance supports modern syntax, you can use the same helper with async/await for even cleaner client scripts.