Hi All, Is there any way to create delay in server...
# suitescript
s
Hi All, Is there any way to create delay in server side script? Thanks
s
please don't do this
👍 2
🤣 1
g
i don’t know what the goal is here, but let’s just say it’s a simulation you want to run … at the very least use an asychronous pattern. i’ll give you an example
only cuz of how
setTimeout
works you can call it with something like this (you can also call it to just count promise ticks if you use
count:0
and no
increaser
function)
Copy code
(callback, duration) => {
    const now = () => ( new Date().getTime() );
    const start = now();
    return promiseUntilThreshold({ count : start, duration }, callback, now);
  }
b
Oh yeah, the infamous server delay script I wrote years ago, some people love it, some people hate it but it never fails to make me smile 🤣. Of course there are more elegant ways to architect the solution but man, it sure would be cool if Netsuite were to give us a proper way to add a synchronous delay.
g
@borncorp i’m curious… what use-case would it be cool for? I can’t think of a reason except for simulation. in that non-production case the above snippet does both
setInterval
and
setTimeout
. I [published it](https://bitbucket.org/suitecoders/workspace/snippets/6XzGko/settimeout-and-setinterval-for-backend) P.S. if you could take down your post (or update it with a warning) that would help me sleep at night. (no offense btw) P.P.S. many of your other posts have helped me sleep at night because they are up so leave those ones up (and thank you)
b
I have only used it when developing a prototype or a proof of concept or something short lived in production. The use case was that I was exporting some data to an external API and the API processed it almost instantly, then I had to run another request 5 seconds later to retrieve more details related to it. It's common when there are multiple system dependent with one another and they don't have a webhook to subscribe to, you just gotta use polling in those cases.
s
the problem with this approach is it is a horrible waste of resouces - unless some line of code in there is blocking? The best practices accepted among this slack is to call out to a http service that simply accepts a 'delay' parameter, waits that long and returns the result. The assumption here is ion the NS side the https call is a blocking operation
👍 1
Someone, maybe @darrenhillconsulting even made such a web service publicly available to us to call in our scripts if desired?
b
I think you are referring to something like https://deelay.me/ , it's a good idea
👍 1
d
Ya, we built something for this.
s
nay, deelay.me and similar are about introducing intentional lag. That is not the typical use case here. The scenario is simpler - to introduce a delay, no invocation of another url involved. Basically, there are reasons to have a blocking delay that have nothing to do with emulating a 'slow network'. One such example (probably the most common) is rate limiting (or call throttling).