how to set timeout when execute new process? #259
Answered
by
andyfleming
itech001
asked this question in
Help needed
-
how to set timeout when execute new process? |
Beta Was this translation helpful? Give feedback.
Answered by
andyfleming
Jan 7, 2025
Replies: 2 comments 2 replies
-
E.g. #!/usr/bin/env zx
const timeout = (p, ms) => {
return new Promise((resolve, reject) => {
p.then(resolve).catch(reject);
setTimeout(() => {
p.kill()
reject(new Error('timeout'));
}, ms);
})
}
const output = await timeout($`sleep 5; ls`, 2000)
console.log(output) output: § zx timeout.mjs
$ sleep 5; ls
file:///.../timeout.mjs:8
reject(new Error('timeout'));
^
Error: timeout
at Timeout._onTimeout (file:///.../timeout.mjs:8:14)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) Changing the command to $ sleep 2; false
Error:
at file:///Users/mbl/timeout.mjs:13:31
exit code: 1 |
Beta Was this translation helpful? Give feedback.
2 replies
-
The latest syntax is... await $`sleep 999`.timeout('5s')
// Or with a specific signal.
await $`sleep 999`.timeout('5s', 'SIGKILL') Updated docs are here: https://google.github.io/zx/process-promise#timeout |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
antonmedv
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The latest syntax is...
Updated docs are here: https://google.github.io/zx/process-promise#timeout