fs.writeFile in a promise, asynchronous-synchronous stuff

前端 未结 10 1566
清歌不尽
清歌不尽 2020-11-30 22:49

I need some help with my code. I\'m new at Node.js and have a lot of trouble with it.

What I\'m trying to do:

1) Fetch a .txt with Amazon products (ASINs) ;<

10条回答
  •  没有蜡笔的小新
    2020-11-30 23:30

    Finally, the latest node.js release v10.3.0 has natively supported fs promises.

    const fsPromises = require('fs').promises; // or require('fs/promises') in v10.0.0
    fsPromises.writeFile(ASIN + '.json', JSON.stringify(results))
      .then(() => {
        console.log('JSON saved');
      })
      .catch(er => {
        console.log(er);
      });
    

    You can check the official documentation for more details. https://nodejs.org/api/fs.html#fs_fs_promises_api

提交回复
热议问题