Is it possible to run `Process`es synchronously?

I’m running into concurrency issues when running my linter process asynchronously, and was wondering if there was a way to run shell Process instances synchronously?

Thanks!

There isn’t; Running a Process object synchronously would block JavaScript execution for all extensions, as all extensions for a workspace use a single event queue of operation.

4 Likes

Is there a way to execute a Process (Process B) on completion of another Process (Process A)? Or is it better to just use a Shell Process to do that?

You can start the second process inside of the first process’s onDidExit callback, but that’s a little bit ugly and, if a third process needs to be started, becomes very hard to read.

Instead, I use a function like this one:

function startProcess(location: string, args: string[], cwd?: string) {
	const options = {
		args,
		cwd
	};
	const process = new Process(location, options);
	process.onStdout(line => console.log(line));

	const onExit = new Promise((resolve, reject) => {
		process.onDidExit(status => {
			console.log(`Exited ${location} with code ${status}`);
			const action = status == 0 ? resolve : reject;
			action(status);
		});
	});

	process.start();
	return onExit;
}

It can be called like this, inside an async function:

await startProcess("/usr/bin/curl", ["https://…", "..."]);
await startProcess("/usr/bin/zip", ["/Users/…"])

If that code were run, the curl process would be started first. Then, after curl exited, the zip process would begin.

2 Likes

You are awesome my friend. Works beautifully. Thank you!