Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

lost promise in async await method

Hi guys, I’m newbie in node.js. I have function, that leads the dialogue with user in console. In newSwagger function I call the bumpDiff function, that shows changes between 2 files.

async function bumpDiff = () => {

exec('bump diff swagger.json newSwagger.json', (err, output) => {
  // once the command has completed, the callback function is called
  if (err) {
      // log and return if we encounter an error
      console.error("could not execute command: ", err)
      return
  }
  // log the output received from the command
  console.log("Output: \n", output)
})
};

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const question = (str) => new Promise(resolve => rl.question(str, resolve));

const steps = {
  start: async () => {
    return steps.changeSwagger();
  },
  changeSwagger: async () => {
    const addSwagger = await request();
    console.log('success');
    const changeSwagger = await question("Would you like to check changes in swagger? Please type yes/no: ");
    if (changeSwagger === 'yes') { return steps.newSwagger(); }
    if (changeSwagger === 'no') { return steps.oldSwagger(); }
    return steps.end();
  },
  newSwagger: async () => {
    console.log('showing changes');
    const diff = await bumpDiff();
    const ask = await question('Would you like to change swagger? Please type yes/no: ');
    if (ask === 'yes') { return steps.changing(); }
    if (ask === 'no') { return steps.end(); }
  },
  changing: async () => {
    const rebuildSwagger = await SwaggerNew();
    console.log('swagger changed successfully');
    return steps.end();
  }, 
  oldSwagger: async () => {
    console.log('No worries, have a nice day');
    return steps.end();
  },
  end: async () => {
    rl.close();
  },
};

steps.start();

The problems is: when bumpDiff is starting, next readline

'Would you like to change swagger? Please type yes/no: '

come faster, then changes appeares. I guess I’m missing something about promises, can you help me find the mistake please.
P.S. all other functions, like ‘request’ and ‘SwaggerNew’ are async.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to turn the callback style exec call into a promise so that await bumpDiff() can work properly.

async function bumpDiff = () => {
  return new Promise((resolve, reject) => {
    exec('bump diff swagger.json newSwagger.json', (err, output) => {
      if (err) {
        console.error("could not execute command: ", err)
        reject(err)
      } else {
        console.log("Output: \n", output)
        resolve(output)
      }
    })
  })
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading