Sleep function in server side script?

ishtein
Mega Contributor

In my server side script, I am triggering an action via an outbound ReST call. The asynchronous operation returns immediately, but provides a URL which I can poll for the operation's completion. I would like to keep the function atomic, so would like to poll the URL, and if the operation hasn't yet completed, would like to wait for a second or so and poll again, and do that for several times (I do not expect the operation to take more than 10-20 seconds, normally not more than 5 seconds) until done.

I am looking for something like setTimeout or setInterval function that I could use on the server, so that I can tell my script to wait, but can't find anything. Am I missing something? Can somebody point me to a solution for this kind of problem?

I have seen a timer activity in Workflow, which would pause the workflow for a specified period of time; it can probably serve as a workaround, but I wanted a programmatic (and programmable) solution. Will be grateful for any guidance!

17 REPLIES 17

Yes, I saw this in the thread that Chuck referenced above. A software developer in me resisted burning processing cycles to generate a delay, but I needed a quick shortcut, and I gave it a try. It seems to be imprecise, but i just needed a delay between attempts to retrieve a ReST status, so it works as a temporary workaround. I emphasize temporary, will investigate the right way later, time permitting.


nowadays, I bet we could use the flow instead for this to reach the same functionality without scripting 😃

//Göran
Feel free to connect:
LinkedIn
Subscribe to my YouTube Channel
or look at my Book: The Witch Doctor's Guide To ServiceNow

tiagomacul
Giga Sage

Random Delay

 

var intDelay = parseInt(Math.random()*10000,10);
gs.sleep(intDelay)

 

 

Sleep

are there any alternative to gs.sleep()?

Hi,

 

alternatively, it's possible to use an event do trigger something