Why is 'await $.flow.delay(10000)' not working?

This topic was automatically generated from Slack. You can find the original thread here.

‘await $.flow.delay(10000)’ is not working

Based on your description, it seems like you’re encountering an issue with the await $.flow.delay(10000) function not working as expected in your Pipedream workflow. This function is designed to pause the execution of your workflow for the specified number of milliseconds—in this case, 10,000 milliseconds or 10 seconds. Here’s a basic example of how to use $.flow.delay correctly within a Pipedream component: export default defineComponent({ async run({ steps, $ }) { // Delay the workflow for 10 seconds (10,000 milliseconds) await $.flow.delay(10000); // Add any subsequent actions here that you want to execute after the delay }, }); Please ensure that: 1. You’re using await with $.flow.delay inside an async function, as shown in the example. 2. There are no syntax errors or issues elsewhere in your code that might be preventing the delay from working as expected. If you’ve confirmed these points and the issue persists, it might be helpful to review the specific context or error messages you’re encountering. If there’s more to the issue that hasn’t been covered, please provide additional details so I can assist you further.

I got this in the log: Would have paused until 2024-03-22T02:06:51.014Z

It seems like you encountered a message in your Pipedream workflow logs indicating a scheduled pause or delay in your workflow execution until a specific future date and time (2024-03-22T02:06:51.014Z). This message typically appears when you’re using a delay function or scheduling functionality within your workflow to pause execution until a specified time. If you intended to pause your workflow until this date and time, then everything is working as expected. However, if this pause was unintended or if you have questions about managing delays in your workflows, you might want to review your workflow’s logic related to delay or scheduling actions. For managing delays or scheduling in your workflows, Pipedream provides built-in support for delaying workflow execution. You can use the $end function to end a workflow early or the Delay app for more complex delay configurations. More information on controlling the flow of your workflows, including delaying execution, can be found here: Delay and Flow Control in Pipedream Workflows. If you need further assistance or have specific questions about implementing delays or scheduling in your workflows, please visit Pipedream Support.

Hi , any idea what could be causing this?

Hey , would you mind elaborate “not working” means?

, I trying to use the $.flow.delay to wait for a few seconds before continuing to execute the code below that. Am I right to use this? I wanted to have the same behavior with Javascript’ss setTimeout

I believe $.flow.delay is not the right method for your usecase, as $.flow.delay will pause the workflow entirely.

If you just want to delay in your code step, you can use Promise.delay from Bluebird library. This is what is usually do when needing delay in my code

import BPromise from 'bluebird'

// To use previous step data, pass the `steps` object to the run() function
export default defineComponent({
  async run({ steps, $ }) {
    console.log('Before delay')

    // Delay for 5 seconds
    await BPromise.delay(5000)

    console.log('After delay')
    // Return data to use it in future steps
    return steps.trigger.event
  },
})

I see thanks Leo.

So I have been using the wrong method all along. My bad. Thank man! :slightly_smiling_face:

Yeah, feel free to check Pipedream documentation for more details

Works great! You rock! Thank you :slightly_smiling_face:

If you don’t want to import a library for three lines of code:

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

await delay(10000);

It sounds like you’re trying to test $.flow.delay() while testing your workflow, is that right? Have you tried deploying the workflow? I think we don’t actually delay the execution while in build mode, in order to make the testing more feasible (hence the console.log() message saying that it would have paused.

Thanks guys for responding. Bluebird library works for me for now. :slightly_smiling_face: