I'm attempting to run a command line tool and parse the output from stdout
in order to do something useful with it in a VSCode extension. I'm attempting to create Flow
objects (defined elsewhere in the code) and append them to this.flows
; however, the function returns before this.flows
is set.
getChildren(element?: vscode.TreeItem): Thenable<Flow[]> | Flow[] | null {
if (element === undefined) {
exec(commandToRun, (error, stdout, stderr) => {
let flowJson = JSON.parse(stdout); // output is expected in json format
for (let i =0; i < jsonString.length; ++i) {
let f = new Flow(jsonString[i][name], flowJson[i]['description'], vscode.TreeItemCollapsibleState.None);
this.flows.push(f);
}
}
}
return new Promise<Flow[]>(resolve => resolve(this.flows));
}
I believe it need to make use of await
or then
, but not sure how to incorporate them to achieve the desired result.
Any help is greatly appreciated, cheers!
It looks like you're using the async version of exec.
Instead, you can use execSync and avoid redundant async-awaits. For example:
let stdout = execSync(commandToRun)
let flowJson = JSON.parse(stdout); // output is expected in json format
for (let i =0; i < jsonString.length; ++i) {
let f = new Flow(jsonString[i][name], flowJson[i]['description'], vscode.TreeItemCollapsibleState.None);
this.flows.push(f);
}