Trigger certain event after timeout

I am working on a slate application. For a slate event, I want to trigger event after 5 seconds but using setTimeout does not work, as it escapes that and returns undefined, and proceed to next step without waiting for set time out.

I tried this

Question originally asked by ap11 on Stack Overflow: palantir foundry - Trigger certain event after timeout - Stack Overflow

UPDATE: You can now use promises in Slate functions.

I’m going to interpret that screenshot as meaning “lines 5-9 should ideally be between lines 11 and 13”. Let me know if that’s an incorrect assumption.

With that assumption in mind, I don’t think that code will ever do what you want it to do. I am pretty sure, though not 100% certain, that the code run in Slate based on an Event trigger is synchronous – it will run immediately and you can’t .then() or await anything before passing the return value to the Action.

If you really just want a 5s delay, there may be a way to accomplish this by (1) an event trigger that causes a Function to run (this function would sleep for 5s), and (2) another event trigger that waits for that Function to complete successfully before executing the Action you actually want to run after the 5s delay. Since you can’t use libraries that depend on the Node runtime in Functions, I think you’ll need to make your own version of setTimeout() – perhaps something like this but please do your own research if you go this route.

An all-Slate option might be to leverage the Code Sandbox widget to contain a function with setTimeout() that gets called when you pass a message into the sandbox, and which passes a message back to Slate when it completes, which in turn triggers some Action.

This really does raise the question of why you need to insert a delay here…Is it to allow some other process to complete? If yes, then could you just hook into that process directly so that whatever action you care about runs when it finishes? Could you do the processing in Functions entirely perhaps so you can use Promises? I’m not sure what might be most appropriate for your situation, but it feels like there may potentially be a different approach.