fs.writeFile in a promise, asynchronous-synchronous stuff

前端 未结 10 1354
清歌不尽
清歌不尽 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:31

    As of 2019...

    ...the correct answer is to use async/await with the native fs promises module included in node. Upgrade to Node.js 10 or 11 (already supported by major cloud providers) and do this:

    const fs = require('fs').promises;
    
    // This must run inside a function marked `async`:
    const file = await fs.readFile('filename.txt', 'utf8');
    await fs.writeFile('filename.txt', 'test');
    

    Do not use third-party packages and do not write your own wrappers, that's not necessary anymore.

    No longer experimental

    Before Node 11.14.0, you would still get a warning that this feature is experimental, but it works just fine and it's the way to go in the future. Since 11.14.0, the feature is no longer experimental and is production-ready.

    What if I prefer import instead of require?

    It works, too - but only in Node.js versions where this feature is not marked as experimental.

    import { promises as fs } from 'fs';
    
    (async () => {
        await fs.writeFile('./test.txt', 'test', 'utf8');
    })();
    
    0 讨论(0)
  • 2020-11-30 23:32
    const util = require('util')
    const fs = require('fs');
    
    const fs_writeFile = util.promisify(fs.writeFile)
    
    fs_writeFile('message.txt', 'Hello Node.js')
        .catch((error) => {
            console.log(error)
        });
    
    0 讨论(0)
  • 2020-11-30 23:32

    For easy to use asynchronous convert all callback to promise use some library like "bluebird" .

          .then(function(results) {
                    fs.writeFile(ASIN + '.json', JSON.stringify(results), function(err) {
                        if (err) {
                            console.log(err);
                        } else {
                            console.log("JSON saved");
                            return results;
                        }
                    })
    
    
                }).catch(function(err) {
                    console.log(err);
                });
    

    Try solution with promise (bluebird)

    var amazon = require('amazon-product-api');
    var fs = require('fs');
    var Promise = require('bluebird');
    
    var client = amazon.createClient({
        awsId: "XXX",
        awsSecret: "XXX",
        awsTag: "888"
    });
    
    
    var array = fs.readFileSync('./test.txt').toString().split('\n');
    Promise.map(array, function (ASIN) {
        client.itemLookup({
            domain: 'webservices.amazon.de',
            responseGroup: 'Large',
            idType: 'ASIN',
            itemId: ASIN
        }).then(function(results) {
            fs.writeFile(ASIN + '.json', JSON.stringify(results), function(err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log("JSON saved");
                    return results;
                }
            })
        }).catch(function(err) {
            console.log(err);
        });
    });
    
    0 讨论(0)
  • 2020-11-30 23:35

    Because fs.writefile is a traditional asynchronous callback - you need to follow the promise spec and return a new promise wrapping it with a resolve and rejection handler like so:

    return new Promise(function(resolve, reject) {
        fs.writeFile("<filename.type>", data, '<file-encoding>', function(err) {
            if (err) reject(err);
            else resolve(data);
        });
    });
    

    So in your code you would use it like so right after your call to .then():

     .then(function(results) {
        return new Promise(function(resolve, reject) {
                fs.writeFile(ASIN + '.json', JSON.stringify(results), function(err) {
                   if (err) reject(err);
                   else resolve(data);
                });
        });
      }).then(function(results) {
           console.log("results here: " + results)
      }).catch(function(err) {
           console.log("error here: " + err);
      });
    
    0 讨论(0)
提交回复
热议问题