Asynchronous Computing with JavaScript Promises

markmmiller09
Kilo Expert

ECMA-262 (6th edition) supports promises natively. It is my understanding that ServiceNow uses Mozilla Rhino which is still on ECMA 5. Is there currently any sort of support for promises within the ServiceNow environment?

A simple use case is as follows: I have a large job, Job A, that currently takes around 5 minutes to finish execution. In a Script Include, I am looking to start Job A via a web service. I'd like to use a promise within the Script Include stating, once Job A has completed successfully, run this code. If it fails, run this code, etc. Are there any API's out there that support this type of behavior?

8 REPLIES 8

drjohnchun
Tera Guru

Wouldn't you be able to do this using a workflow? Sure, it's not the same as promises and not as flexible, but you might be able to achieve what you're describing.



Another possibility that comes to my mind is doing a loopback web service call with callbacks for success and fail cases.



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award


Hey John,



Thanks for the reply. I've considered the workflow idea but was wondering if there is a more efficient way to handle this. Maybe one day ServiceNow will support promises.



Thanks


Geni1
Tera Contributor

Servicenow does support promises to implement a promise please see the example code below:



asyncFunction = new function(inputs ........)


return new Promise(function(response, reject){



Some Logic.....


response(output);


Else


reject(output);



})


}



Calling the asyncFunciton:



asyncFunction(input).then(function(res){



Some Logic for succes



}, function(err){



Some Logic for error handling



});


Hi geni,

Have you used this successfully in a ServiceNow environment? What was the context where it was used? Would you mind providing a sample script that's working for you? I'm testing this with a simple example, and it is not working for me in a catalog client script run in the non-portal UI in a Jakarta environment. I could just be missing something, of course.

Thanks!