Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
Get asynchronous value's return on C2

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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda