Yes, you’ll want to wait between each iteration of checking for successful completion of the job.
Try this code:
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();
@Phil_Seeman
thanks for your help!
I tried it.Unfortunately an error below occured.
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.
@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.
@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?
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.
Hi @Tetsuo_Kawakami , it’s great to meet you, and thank you for highlighting such a valid use case.
@dbrdak 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.
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!
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: