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

Fagner Brack

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.

Fagner Brack

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use Typescript Async/ await with promise in Node JS FS Module

From Dev

fs.writefile only execute in last function in node js

From Dev

fs.writefile only execute in last function in node js

From Dev

How to use fs.write function of node.js?

From Dev

How to ensure all directories exist before to fs.writeFile using node.js

From Dev

node.js fs.writeFile Not Completely Overwriting File

From Dev

Node.js fs.writeFile() not ovewriting file

From Dev

How to promisify a node.js addon method

From Dev

how to use bluebird to promisify node-rest-client

From Dev

node - fs.writeFile creates a blank file

From Java

Node.js 7 how to use sequelize transaction with async / await?

From Dev

Execute code after fs.writeFile using async/await

From Dev

how to use where function in node js

From Dev

Node js: Bluebird Promisify in Mongoose middleware

From Dev

Node js: Bluebird Promisify in Mongoose middleware

From Dev

Prevent file corruption on exiting node in middle of fs.writeFile

From Dev

node's fs.writeFile does not overwrite previous contents

From Dev

node.js fs.exists() will be deprecated, what to use instead?

From Dev

Node JS - I Can't Use FS.Stat Synchronously

From Dev

How to promisify a MySql function using bluebird?

From Dev

Node.JS WriteFile - File of specific size

From Dev

Node.JS WriteFile - File of specific size

From Dev

How to use async await function object in Javascript?

From Dev

Error with NodeJS FS (writeFile)

From Dev

In fs.writeFile([option]), how an "options parameter" generally work?

From Dev

How to use data returned by an asynchronous function in Node.js?

From Dev

how to use inner's result in outer function in node.js

From Dev

how to use inner's result in outer function in node.js

From Dev

How to use async await in Nuxt.js?

Related Related

  1. 1

    How to use Typescript Async/ await with promise in Node JS FS Module

  2. 2

    fs.writefile only execute in last function in node js

  3. 3

    fs.writefile only execute in last function in node js

  4. 4

    How to use fs.write function of node.js?

  5. 5

    How to ensure all directories exist before to fs.writeFile using node.js

  6. 6

    node.js fs.writeFile Not Completely Overwriting File

  7. 7

    Node.js fs.writeFile() not ovewriting file

  8. 8

    How to promisify a node.js addon method

  9. 9

    how to use bluebird to promisify node-rest-client

  10. 10

    node - fs.writeFile creates a blank file

  11. 11

    Node.js 7 how to use sequelize transaction with async / await?

  12. 12

    Execute code after fs.writeFile using async/await

  13. 13

    how to use where function in node js

  14. 14

    Node js: Bluebird Promisify in Mongoose middleware

  15. 15

    Node js: Bluebird Promisify in Mongoose middleware

  16. 16

    Prevent file corruption on exiting node in middle of fs.writeFile

  17. 17

    node's fs.writeFile does not overwrite previous contents

  18. 18

    node.js fs.exists() will be deprecated, what to use instead?

  19. 19

    Node JS - I Can't Use FS.Stat Synchronously

  20. 20

    How to promisify a MySql function using bluebird?

  21. 21

    Node.JS WriteFile - File of specific size

  22. 22

    Node.JS WriteFile - File of specific size

  23. 23

    How to use async await function object in Javascript?

  24. 24

    Error with NodeJS FS (writeFile)

  25. 25

    In fs.writeFile([option]), how an "options parameter" generally work?

  26. 26

    How to use data returned by an asynchronous function in Node.js?

  27. 27

    how to use inner's result in outer function in node.js

  28. 28

    how to use inner's result in outer function in node.js

  29. 29

    How to use async await in Nuxt.js?

HotTag

Archive