Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run script of my vorpal commands does not work on async functions #346

Open
ghost opened this issue Jan 19, 2020 · 2 comments
Open

Run script of my vorpal commands does not work on async functions #346

ghost opened this issue Jan 19, 2020 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 19, 2020

I tried to write a script runner, but it does not wait for async functions:

vorpal
    .command('run <file_name>', 'Run a script file')
    .validate(function (args) {
        if (fs.existsSync(args.file_name)) {
            return true;
        } else {
            return `File does not exists '${args.file_name}'`;
        }
    })
    .action(function (args, callback) {
        const scriptLines = fs.readFileSync(args.file_name).toString().split('\n');
        for (const line of scriptLines) {
            if (line.trim() !== '') {
                vorpal.execSync(line);
            }
        }
        callback();
    });

Tried that too, but it exits instantly:

vorpal
    .command('run <file_name>', 'Run a script file')
    .validate(function (args) {
        if (fs.existsSync(args.file_name)) {
            return true;
        } else {
            return `File does not exists '${args.file_name}'`;
        }
    })
    .action(async function (args, callback) {
        const scriptLines = fs.readFileSync(args.file_name).toString().split('\n');
        for (const line of scriptLines) {
            if (line.trim() !== '') {
                await vorpal.exec(line);
            }
        }
        callback();
    });

Any ideas?

@ghost
Copy link
Author

ghost commented Jan 22, 2020

Maybe before creating the Promise, there should be a test is it is async function.

function isAsync(func) {
    return func.constructor.name === "AsyncFunction";
}

@lannierose
Copy link

Late to the party here, but I solved this problem using sync-rpc to put my asynchronous operations into a worker thread. It's like magic! The main thread sees synchronous operation while the worker thread does the async stuff. Sounds complicated (for someone like me who never used workers before) but just follow the example in sync-rpc and it's not very difficult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant