Home > Software engineering >  lost promise in async await method
lost promise in async await method

Time:11-03

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.

CodePudding user response:

You are mixing two "styles" the callback approach in the "exec" function and you are trying to await that bumpDiff that is not returning a Promise (async/await approach), take a look to utils.Promisify helper function.

From NodeJS docs:

const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);

async function lsExample() {
  const { stdout, stderr } = await exec('ls');
  console.log('stdout:', stdout);
  console.error('stderr:', stderr);
}
lsExample();

CodePudding user response:

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)
      }
    })
  })
};
  • Related