The CreatorCon Call for Content is officially open! Get started here.

Using Promises inside Fix Scripts?

sthous
Giga Contributor

Hi,

I am working on a Fix Script that is supposed to use asynchronous style coding in order to carry out a lot of outbound REST requests, but I was surprised to see that the Promise identifier could not be found ? The line is log says: 


ReferenceError: "Promise" is not defined.

and basically I have something like this that is part of the Script Include:

new Promise(function(resolve, reject) {

});

Is there something I'm missing here or promises really can't be used inside fix scripts ? Thanks.

4 REPLIES 4

Community Alums
Not applicable

I am having the same issue.  Have you figured out if this is expected behavior?

Please check this and see if it helps

http://www.john-james-andersen.com/blog/service-now/angular-servicenow-tutorial-4-promises.html


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Well not really. For my problem, I found that using a scheduled script that executes every few minutes would be a better solution anyway. Regarding Promises -  JS on the server side is actually executed on the old Rhino interpreter (like from 2008) which doesn't have any support for async related operations(setTimeout, Promise etc). Later on, I found that by default the app is executing in Compatibility Mode (System Applications -> Applications > Your Application -> JavaScript Mode) and there is an option to choose ES5. I haven't tried this, it might add support for some async operations like setTimeout/setInterval, but I doubt that Promises would still work as they are an ES6 concept. Good luck, and please let me know if you find solution for your problem.

Jean Ferreira
Giga Guru

If the purpose is wait a time for the next execution you can just use the synchronized function:

var seconds = 10;
gs.sleep(seconds*1000);

Or you can try to use callback instead of Promise

function wait(time, callback) {
  gs.sleep(time);
  if (callback && (typeof callback == "function")) {
    return callback();
  }
}