0

I want to run func2 run only after func1 has finished.

{
func1();
func2();//
}

But when func1() starts, func2() does not wait for func1() to get finished. func2() run simultaneously with func1(), which causes run time error, because func2() need some input from func1(). I also tried using async module, but got no success.

Edit: I run the function check(), which call two functions prompt() and copyProject(). copyProject() should run only after prompt() function has finished, but it start executing simultaneously.

var check = function check (){
        async.series([
            function(callback){
                prompt();
                callback(null);
            },
            function(callback){
                copyProject();
                callback(null);
            }
        ]);
};

var prompt = function prompt(){
      var prompts = [{
      name: "projectName",
      message: "What is the name of your project?"
    },{
      name: "authorName",
      message: "What is your name?",
    }];
    inquirer.prompt(prompts, function( answers ) {
      for (var key in answers) {
        if (answers.hasOwnProperty(key)) {
          console.log(key + " -> " + answers[key]);
          infoToRender[key] = answers[key]
        }
      }
});
};

var copyProject = function copyProject (){
 // code to copy some files from one location to another based on input from prompt function
};
4
  • If a function does something asynchronous, it does take a callback or return a promise. In your attempt at using async, you'll have to pass the callback to the asynchronous function, not call it immidiately. Please show us your actual code. Commented May 18, 2015 at 5:19
  • You should be able to achieve your goal with async.series. Could you elaborate on what you expected as a result from using async.series and why this was not successful for you? Commented May 18, 2015 at 5:21
  • func1 must be asyc, and if so, it takes a function as an argument func1(done). Call your callback from inside that done Commented May 18, 2015 at 5:23
  • @Macro My only requirement is that the copyProject() function get executed only after prompt() function has finished executing. Commented May 18, 2015 at 5:39

1 Answer 1

3

Your function prompt is itself an asynchronous function: The call to inquirer.prompts causes the function to wait and this makes it asynchronous. Since your prompt function does not use a callback, the async module can not know when prompt has finished: In contrast, it runs prompt and this function immediately returns. Hence async right afterwards runs copyProject.

What you need to do to solve this issue is to introduce a callback function in prompt that async "waits" for. So basically something like this:

var prompt = function (callback) {
  // Do your stuff here...
  inquirer.prompt(prompts, function (answers) {
    // Your for loop here...
    callback(null);
  });
};

Then, within async you need to do:

async.series([
  function (callback) {
    prompt(callback);
  },
  function (callback) {
    copyProject();
    callback(null);
  }
]);

Then copyProject is only run when prompt has been completed. Please note that async.series will not wait for copyProject to finish, in case this is an asynchronous function, as again, it does not take a callback. So same pattern applies here.

Sign up to request clarification or add additional context in comments.

2 Comments

BTW, is it possible to achieve that goal without using async module.
Yes: Instead of the callback, simply run copyProject in the callback of your inquirer.prompt function. That's it :-). To put it in other words: If you have asynchronous functions and you want them to run sequentially, nest them. If you want to run them in parallel put them next to each other, as you would do with synchronous ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.