Async/Await is not working with node 4.x. Can i have the alternate?

后端 未结 3 1576
走了就别回头了
走了就别回头了 2021-01-27 21:39

Getting below error when i try to use async/await with NodeJs 4.x. Any issue with the below sample code or should i use alternate ?

async function main ()

相关标签:
3条回答
  • 2021-01-27 22:14

    try installing asyncawait. It should work for older node versions. Other alternatives are using a callback or promises.

    you'll have to require it

    1. npm install asyncawait

    2. require modules.

      var async = require('asyncawait/async');

      var await = require('asyncawait/await');

    3. perform operations.

      (async function () { const intgetIDvalue = await fntest(getID); })();

    0 讨论(0)
  • 2021-01-27 22:21

    try async.js http://caolan.github.io/async/

    var async = require("async");  
    async.map(['file1','file2','file3'], fs.stat, function(err, results) {
        // results is now an array of stats for each file
    });
    
    async.parallel([
        function(callback) { ... },
        function(callback) { ... }
    ], function(err, results) {
        // optional callback
    });
    
    0 讨论(0)
  • 2021-01-27 22:22

    Node 4.x Does not support async await out of the box. In your cade I believe that the easiest way to add support to it will be though babel-node.

    Run npm install babel-cli --save-dev
    

    and add this to your package.json

    "scripts": {
        "start": "babel-node --presets env src/index.js"
      },
    

    you may need to change src/index.js by your entry point file.

    then install the presets

    npm install babel-preset-env --save-dev
    

    then start your project using

    npm start
    

    this shall do the trick.

    The advantage of this over installing asyncawait is that you don't need to change anything in your code, as asyncawait gives you functions that have a similar behavior to async and await operator but does not really transpile your code.

    PS: you can specify you presets in a .babelrc file too, feel free to read more about it here https://babeljs.io/docs/en/config-files#file-relative-configuration

    0 讨论(0)
提交回复
热议问题