# Is there a way to wait until method is completed?

**URL:** <https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625>\
**Category:** API & Developers\
**Created:** [10 September 2025 05:02 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625 "2025-09-10T05:02:39Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tetsuo\_Kawakami](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/tetsuo_kawakami/32/84271_2.png) [@Tetsuo\_Kawakami](https://forum.asana.com/u/Tetsuo_Kawakami)\
**Post date:** [10 September 2025 05:02 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/1 "2025-09-10T05:02:39Z")

</div>

I’m writing a code for script actions.  
I want to get subtask’s gids after duplicating a task with subtasks.  
So I worte a code below at the begining.

```auto
body = {
      "data": {
        "name": task.data.name,
        "include": ["notes","assignee","subtasks","attachments","tags","followers","projects","dependencies","parent"]
      }
    };
const newTask = await tasksApiInstance.duplicateTask(body, task_gid);

```

After that, I’ll confirm whether a duplication has finished.  
I assume using jobsApiInstance.getJob() with loop like below.

```auto
i = 0;
while(i<10){
  const jobStatus = jobsApiInstance.getJob(......);
  if(jobStatus.data.status === "succeeded"){
    break;
  }
  i++;
}

```

But I’m worried about requiring a lot in a short time.  
Is there a way to wait like timer?

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [10 September 2025 07:08 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/2 "2025-09-10T07:08:20Z")

</div>

Hi @Tetsuo_Kawakami,

Yes, you’ll want to wait between each iteration of checking for successful completion of the job.

Try this code:

```auto
async function run() {
  const task = await tasksApiInstance.getTask(task_gid);
  body = {
      "data": {
        "name": task.data.name,
        "include": ["notes","assignee","subtasks","attachments","tags","followers","projects","dependencies","parent"]
      }
    };
  const jobInfo = await tasksApiInstance.duplicateTask(body, task_gid);
  await waitForJobCompletion(jobInfo.data.gid);
  log("The job finished!")
}

const sleep = ms => new Promise(res => setTimeout(res, ms));

async function waitForJobCompletion(jobId) {
  for (let i = 0; i < 10; i++) {
    const jobStatus = await jobsApiInstance.getJob(jobId);
    if (jobStatus.data.status === "succeeded") {
      return jobStatus;
    }
    if (i < 9) { // don't wait after the last attempt
      await sleep(5000); // wait 5 seconds before next iteration
    }
  }
  throw new Error("Timed out waiting for job");
}

// To function properly, the script must end with returning a Promise via run().
run();

```

---

<div class="post-metadata">

**Author:** ![Tetsuo\_Kawakami](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/tetsuo_kawakami/32/84271_2.png) [@Tetsuo\_Kawakami](https://forum.asana.com/u/Tetsuo_Kawakami)\
**Post date:** [10 September 2025 12:30 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/3 "2025-09-10T12:30:53Z")

</div>

@Phil_Seeman  
thanks for your help!  
I tried it.Unfortunately an error below occured.

```auto
 Encountered an error: {"name":"Error","message":"setTimeout is not supported in this environment","stack":" at <anonymous> (eval.js)\n at Promise (native)\n at sleep (eval.js)\n at waitForJobCompletion (eval.js:33)\n"}

```

That would mean Asana doesn’t allow us to use setTimeout().  
I’ll find out another way.

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [10 September 2025 16:24 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/4 "2025-09-10T16:24:43Z")

</div>

That’s odd, it worked perfectly for me, including the `setTimeout`. Not sure why you’re getting that error.

---

<div class="post-metadata">

**Author:** ![Tetsuo\_Kawakami](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/tetsuo_kawakami/32/84271_2.png) [@Tetsuo\_Kawakami](https://forum.asana.com/u/Tetsuo_Kawakami)\
**Post date:** [10 September 2025 21:49 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/5 "2025-09-10T21:49:27Z")

</div>

@Phil_Seeman  
Thanks for your reply.  
Yes. That’s very weird.Which did you execute it in script actions or in your local env?  
I run it in my phoenix env.  
Anyway I’ll try again and struggle.

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [10 September 2025 23:38 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/6 "2025-09-10T23:38:40Z")

</div>

I ran it in script actions in my Phoenix environment!

@Mikhail_Melikhov any idea why it worked in my Phoenix script action but not @Tetsuo_Kawakami’s?

---

<div class="post-metadata">

**Author:** ![Tetsuo\_Kawakami](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/tetsuo_kawakami/32/84271_2.png) [@Tetsuo\_Kawakami](https://forum.asana.com/u/Tetsuo_Kawakami)\
**Post date:** [11 September 2025 04:16 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/7 "2025-09-11T04:16:04Z")

</div>

I noticed that sometimes it works and other times it doesn’t.  
When it works, a job status is succeeded at the first iteration. So sleep() doesn’t run.

In the end, how can we wait in script action?  
Does anyone have a solution for it?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Melikhov](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/mikhail_melikhov/32/288769_2.png) [@Mikhail\_Melikhov](https://forum.asana.com/u/Mikhail_Melikhov)\
**Post date:** [11 September 2025 09:17 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/8 "2025-09-11T09:17:42Z")

</div>

Hi @Phil_Seeman TIL that native JS timers are indeed not allowed in action scripts.

Is it possible that the tests you ran succeeded simply because the job completed quickly enough that a `sleep()` function call wasn’t needed?

---

<div class="post-metadata">

**Author:** ![Tetsuo\_Kawakami](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/tetsuo_kawakami/32/84271_2.png) [@Tetsuo\_Kawakami](https://forum.asana.com/u/Tetsuo_Kawakami)\
**Post date:** [11 September 2025 09:53 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/9 "2025-09-11T09:53:08Z")

</div>

@Phil_Seeman  
Please don’t be offended, but I turned off Solution check because the problem isn’t solved.

@Mikhail_Melikhov  
It’s nice to meet you.I think it’s the first contact with you.  
I think we should avoid requiring a lot in a short time to confirm a job status.  
Do you have any idea to wait between each iteration in script action?

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [11 September 2025 13:38 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/10 "2025-09-11T13:38:54Z")

</div>

> [@Mikhail\_Melikhov](#):
>
> native JS timers are indeed not allowed in action scripts.

Ah, well Ok!

> [@Mikhail\_Melikhov](#):
>
> Is it possible that the tests you ran succeeded simply because the job completed quickly enough that a `sleep()` function call wasn’t needed?

Yes that’s possible.

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [11 September 2025 13:41 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/11 "2025-09-11T13:41:02Z")

</div>

> [@Tetsuo\_Kawakami](#):
>
> I turned off Solution check

No worries!

I can think of a few other ideas for waiting, but I don’t know if any of them are supported in this particular environment so I think we should wait (no pun intended) for some guidance from Asana, who knows what can and can’t run here.

---

<div class="post-metadata">

**Author:** ![Larry\_Berger\_lpb](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/larry_berger_lpb/32/276017_2.png) [@Larry\_Berger\_lpb](https://forum.asana.com/u/Larry_Berger_lpb)\
**Post date:** [12 September 2025 04:31 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/12 "2025-09-12T04:31:50Z")

</div>

> [@Phil\_Seeman](#):
>
> I think we should wait (no pun intended) for some guidance

“Helpful” guidance has arrived. Please try one or more of:

- Pace back and forth in your office while repeatedly looking at your watch
- [Cue this](https://youtube.com/shorts/73tGe3JE5IU?si=E31pxbdmhlLoMW7x) (turn on sound)
- Light up a cigarette; that always used to make the bus you were waiting for appear right away so it should hasten the script’s completion.

Sorry, @Tetsuo_Kawakami!

Larry

---

<div class="post-metadata">

**Author:** ![Mikhail\_Melikhov](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/mikhail_melikhov/32/288769_2.png) [@Mikhail\_Melikhov](https://forum.asana.com/u/Mikhail_Melikhov)\
**Post date:** [12 September 2025 12:48 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/13 "2025-09-12T12:48:26Z")

</div>

Hi @Tetsuo_Kawakami , it’s great to meet you, and thank you for highlighting such a valid use case.

@Dominik_Brdak identified a potential approach to simulate native timeouts, but before sharing, we’d like to confirm internally whether this is the most suitable solution. We’ll get back to this shortly with an update.

---

<div class="post-metadata">

**Author:** ![Stephen\_Li](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/stephen_li/32/279555_2.png) [@Stephen\_Li](https://forum.asana.com/u/Stephen_Li)\
**Post date:** [12 September 2025 13:14 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/14 "2025-09-12T13:14:23Z")

</div>

Chiming in to say I also had this issue (my code must not have ever invoked setTimeout while testing so I didn’t realize it wasn’t supported and then dropped the script on like 30 projects with thousands of tasks lol). Really looking forward to the (potential, hopefully) solution!

---

<div class="post-metadata">

**Author:** ![Dominik\_Brdak](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/dominik_brdak/32/281949_2.png) [@Dominik\_Brdak](https://forum.asana.com/u/Dominik_Brdak)\
**Post date:** [15 September 2025 09:18 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/15 "2025-09-15T09:18:46Z")

</div>

Hi everyone!

After checking with our developers, I can confirm that using a custom sleep / setTimeout function is supported. That said, please remember that script actions have a maximum runtime of 60 seconds. This limit is in place for security reasons – for example, to protect against mistakes such as infinite loops.

If your workflow requires more time, consider breaking down a complex rule into smaller rules that trigger each other. This can help separate your logic into multiple steps and avoid hitting the 60-second timeout.

Here’s an example of a simple sleep function shared by our developers:

```auto
const sleep = async (ms) => {
    log('Waiting...');
    const start = Date.now()
    let currentTime = Date.now()
    while(currentTime - start < ms){
        currentTime = Date.now()
    }
};

```

---

<div class="post-metadata">

**Author:** ![Tetsuo\_Kawakami](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/tetsuo_kawakami/32/84271_2.png) [@Tetsuo\_Kawakami](https://forum.asana.com/u/Tetsuo_Kawakami)\
**Post date:** [16 September 2025 00:13 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/16 "2025-09-16T00:13:43Z")

</div>

@Dominik_Brdak  
Thank you for the solution.  
I confirmed it on my side.It worked well!

---

<div class="post-metadata">

**Author:** ![Tetsuo\_Kawakami](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/tetsuo_kawakami/32/84271_2.png) [@Tetsuo\_Kawakami](https://forum.asana.com/u/Tetsuo_Kawakami)\
**Post date:** [23 September 2025 00:14 UTC](https://forum.asana.com/t/is-there-a-way-to-wait-until-method-is-completed/1096625/17 "2025-09-23T00:14:24Z")

</div>

Closed on 23 September 2025 00:14 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
