我很好奇你应该如何为返回回调的函数设置超时,如果回调永远不会被触发,则承诺永远不会解决。
const mockCliAsync = (argv, stdio, timeout) => {
return new Promise((resolve, reject) => {
let timedout = false
const timeoutTimer = setTimeout(() => {
timedout = true
kill()
return reject(new Error('timeout'))
}, timeout)
const kill = mockCli(argv, stdio, (error, result) => {
if (!timedout) {
clearTimeout(timeoutTimer)
if (error) return reject(error)
return resolve(result)
}
})
})
}