Get asynchronous value's return on C2

237 views Asked by At

Here is the code I'm using:

const { exec } = require("child_process");

exec("ls -la", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

It returns "0" and then logs in the console the correct result.

But how to directly return the result instead of console.log?

Some answers have pointed out how to convert it into a Promise.

2

There are 2 answers

3
Geoduck On

The problem you are experiencing is that it takes time to run an OS command (perhaps indefinite amount of time). Javascript will not just halt execution until this is done, so it is run asynchronously.

A value must be returned and must be returned to the caller before it completes. Using promises, the value returned is a promise.

Since you are calling with the Construct 2 Engine which does not have a mechanism for asynchronous calls, you will not get a result directly.

However, Construct 2 engine has a mechanism for calling back into it, using the Function object

Your javascript should look like this:

const { exec } = require("child_process");

function ls(callback) {
    exec("ls -la", (error, stdout, stderr) => {
        if (c2_callFunction)
            c2_callFunction(callback, [error, stdout, stderr]);
    });
}
ls('lsresult')

you can execute like this :

Browser.ExecJS("")

But, to get the results, you must define a Function object called 'lsresult', add parameters to the function (error, stdout, stderr) and handle the results there.

Documentation is here: https://www.construct.net/en/construct-2/manuals/construct-2/plugin-reference/function

9
Gregorio Palamà On

You can't return a value from that callback, because it would not be passed to anything. What you can do is defining a Promise that passes the stdout to the resolve method. Here's an example:

    const { exec } = require("child_process");
    function ls() {
        return new Promise((resolve, reject) => {
            exec("ls -la", (error, stdout, stderr) => {
                if (error) {
                    console.log(`error: ${error.message}`);
                    reject(error.message);
                }
                if (stderr) {
                    console.log(`stderr: ${stderr}`);
                    reject(stderr);
                }
                console.log(`stdout: ${stdout}`);
                resolve(stdout);
            });
        });
    }

What I am doing here is defining a function that creates a new Promise. The Promise will execute your code (the ls -la call), and will fire an exception if there is an error, rejecting the Promise, or it will solve the Promise if everything is fine, passing the stdout value.

You can then use this Promise with something like this:

ls().then((out) => {
    console.log(out);
})

the out variable will contain your stdout.

If you want some function that returns that value, it should be awaited from this function. An example could be this:

async function unwrapLs() {
    const stdout = await ls();
    return stdout;
}

Note that you can only call unwrapLs() from inside an async function, because you have to await for its value. In fact, this would be equivalent to calling ls() by awaiting it, but you can only do it from inside an async function.