Keep system asynchronous using execSync node.js

30 Views Asked by At

There is a node.js application with the following request processing:

app.get('/api/createMachine', async (req, res) => {
    let result = execSync('lxc create ubuntu: virtual7').toString()
    res.send('create')
})

execSync in this case executes a command to create an lxd container.

The problem is that when execSync is executed, node.js starts processing it synchronously, and stops executing other requests until the end of this execution.

If you use just exec, the command will not be executed.

exec('lxc launch ubuntu: virtual7', (error, stdout, stderr) => {
            if (error) {
                console.log(`error: ${error.message}`);
                return;
            }
            if (stderr) {
                console.log(`stderr: ${stderr}`);
                return;
            }
            console.log(`stdout: ${stdout}`);

    });

Is it possible to use execSync, or other ways to execute commands without losing asynchrony?

0

There are 0 best solutions below