简体   繁体   中英

How to use node js util.promisify with the writeFile fs function and await

How can I convert this function to use the async/await style:

it.only("should bump the 'minor' version attribute", () => {
  const writeFile = util.promisify(require("fs").writeFile);
  return writeFile("bump-minor.json", "contents").then(function() {
    console.log('done');
  });
});

I tried using this but it doesn't work:

const writeFile = util.promisify(require("fs").writeFile);
await writeFile("bump-minor.json", "contents");
console.log('done');

It shows the following error on line 2:

Parsing error: Unexpected token writeFile

If I add "async" to the mocha test function:

it.only("should bump the 'minor' version attribute", async () => {
  const writeFile = util.promisify(require("fs").writeFile);
  return writeFile("bump-minor.json", "contents").then(function() {
    console.log('done');
  });
});

Then I get this error on line 1:

Parsing error: Unexpected token =>

I'm probably missing some fundamental of how async/await and util.promisify works together in node

Using node 8.7.0.

I was running eslint before mocha in "npm test":

"scripts": {
  "test": "eslint *.js \"src/**/*.js\" \"test/**/*.js\" && mocha"
}

Removing the "eslint" call fixe the problem:

"scripts": {
  "test": "mocha"
}

For some reason, eslint was failing async/await syntax with mocha

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM