I need some help with my code. I'm new at nodejs and have a lot of trouble with it. What i'm trying to do:
- 1)fetch a .txt with Amazon products(ASIN's)
2)fetch all products with the amazon-product-api package
3)save each product in a .json file
My code is not working. I think i messed up with this asynchronous-synchronous stuff - help me!
var amazon = require('amazon-product-api');
var fs = require('fs');
var client = amazon.createClient({
awsId: "XXX",
awsSecret: "XXX",
awsTag: "888"
});
var array = fs.readFileSync('./test.txt').toString().split('\n');
for (var i = 1; i < array.length; i++) {
var ASIN = array[i];
return 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);
});
};
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:So in your code you would use it like so right after your call to
.then()
say
https://nodejs.org/api/util.html#util_util_promisify_original
this is less prone to bugs than the top-voted answer
As of 2018...
...the correct answer is to use async/await with the native
fs
module. Upgrade to Node.js 10 (already supported by major cloud providers) and do this:Do not use third-party packages and do not write your own wrappers, that's not necessary anymore.
As of Node
11.1.0
, you will get a warning that this feature is experimental, but it works just fine and it's the way to go in the future.For easy to use asynchronous convert all callback to promise use some library like "bluebird" .
Try solution with promise (bluebird)
Update Sept 2017:
fs-promise
has been deprecated in favour offs-extra
.I haven't used it, but you could look into fs-promise. It's a node module that: