Dashboards & Visualizations

Is Async.sleep() available to Javascript

bowesmana
SplunkTrust
SplunkTrust

I have some JS that is iterating a result set with on('data'). Inside that loop, I am setting up a new search with SearchManager and executing that search. I need to wait for that search to finish before I do the next iteration of the original result row.

I was hoping to be able to do something like

innerResults.on('data', function() {
  while (!innerResults.hasData()) {
    Async.sleep(1000, ...);
  }
  // Now process inner result data and then go to outer result next iteration
)};

I can't find out what to include in my require block at the top of the JS
Any ideas?

christoffertoft
Communicator

Any success with this? I'm stuck at the same issue

0 Karma

KailA
Contributor

Hi,

I don't know what to include in your require block but I can show how I'm doing a sleep in my JS

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

And after creating this function you can use it like this await sleep(1000);

Let me know if it helps you

0 Karma

bowesmana
SplunkTrust
SplunkTrust

I saw this type of post elsewhere, but I can't seem to get the await part, as it says it can only be used in an async function. If I don't use await, then the sleep call does not sleep at all.

I'm a JS noob 😞

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...