0

Working on a project where I call multiple functions featuring setTimeout. In the below code, I can't seem to get my code to run in order. For example, call one function, then the next, then the next. With each waiting for the other to finish.

Feel free to change my code, as I would love to learn more regarding the matter.

**UPDATE

Since it was tough to explain my problem, I figured I'll just post the full code. In the greet page, I ask prompt the user a question using inquirerJS. When I ask the user, my third function fires off, without waiting for the promise to resolve.

TYPE PAGE

module.exports = {
  writer: function(phrase, delaySpeed) {
    let total = 0;
    let empty = [];
    let promises = []
    for (let i = 0; i < phrase.length; i++) {
      total += delaySpeed;
      // new promise for each character
      let promise = new Promise(function(resolve, reject) {
        setTimeout(() => {
          empty.push(phrase.charAt(i));
          process.stdout.write(chalk.blue.bold(empty[i]));
          if (empty.length === phrase.length) { //if complete
            process.stdout.write('\n'); //puts on separate line
          }
          // assuming above writes are synchronous can now resolve promise
          resolve()
        }, total);

      });
      // push new promise to array
      promises.push(promise)
    }
    // return the all() promise
    return Promise.all(promises)// add another then() if you need to return something to next then() in App()
  }
}

GREET PAGE


const inquirer = require('inquirer');
let intro = require('./type.js');
let chalk = require('chalk');
let type = require('./type.js');

let greet = function(){
    let questions = [{
        type: 'input',
        name: 'firstName',
        message: 'what your name',
    }];

    inquirer
        .prompt(questions)
        .then(answers => {
            setTimeout(function(){
                type.writer(`Hello ${answers.firstName}`,100);
            },500);
        }
    )
}
module.exports = greet;

MAIN APP PAGE

let initialGreeting = require('./user.js');



function App(){
    function second(){
        return new Promise(function(resolve,reject){
            setTimeout(function a(){
                initialGreeting();
                 resolve();
             }, 200);
        }
    )
function third(){
    console.log('finished')
}
    type.writer('abc',100).then(second).then(third);

};

App();

4
  • Sorry Neil. I edited the code. Commented Nov 19, 2018 at 4:52
  • are you trying to chain to the result of greet()? Commented Nov 19, 2018 at 4:55
  • There are a lot of functions here that you assume we understand. For example - is type.writer asynchronous (it looks like it takes time parameter)? It would help if you could boil it down to a runnable snippet. Commented Nov 19, 2018 at 4:56
  • Still not clear what you are asking. I mean you don't return new Promise() or actually show using the function, but that's the "best guess" to an actual problem I see here. Commented Nov 19, 2018 at 4:56

1 Answer 1

1

Return you new Promise:

let greet = function(){
    let questions = [{
        type: 'input',
        name: 'firstName',
        message: 'what your name',
    }];
    inquirer
        .prompt(questions)
        .then(answers => {
            return new Promise(function(resolve,reject) {
                setTimeout(function(){
                    type.writer(`Hello ${answers.firstName}`,100);
                    resolve();
                },500);
            }).then(function(){
                console.log("run after")
            });

        }
    )
}
module.exports = greet;
Sign up to request clarification or add additional context in comments.

Comments

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.