-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
Description
When I pass an async typescript function into a command's action() method, the async function is called correctly, but is not run to the end, as the program does not wait when awaiting another function. It simply ends execution. I've seen a similar question here, but it was not answered, as far as I understand.
Consider this example:
#!usr/bin/env node
import * as commander from 'commander';
import axios from 'axios';
async function run() : Promise<void> {
var response = await axios.get("https://dummyimage.com/600x400/000/fff", { "responseType": "arraybuffer" });
if (response.status === 200) {
console.log(`Downloaded file: ${response.data.byteLength} bytes.`)
}
else {
console.error('Error downloading');
}
}
async function main() {
commander
.command("do", "Test")
.action(run)
.parse(process.argv);
// await run();
console.log('finished');
}
try {
main();
}
catch (error) {
console.error(error);
}
If you run the example above, you can see that finished is called but the download is never finished. Calling await run() without the help of commander works, though.
So, is it possible to use commander with typescript's async functions?
Reactions are currently unavailable