I'm new to Node and this is the first time I've tried to use a promise library. With that said ,this is the simple example I'm following: /
This is the code that executes without error however as expected it logs "done" prior to finishing the file read and looping through the array.
var Promise = require('bluebird');
var Converter = Promise.promisifyAll(require("csvtojson")).Converter;
var converter = new Converter({});
var Client = Promise.promisifyAll(require('node-rest-client')).Client;
var client = new Client();
var responseArray = [];
converter.fromFile("./ToterFeed.csv", function(err,result){
for (var i = 0, len = result.length; i < len; i++) {
var args = {
data: JSON.stringify(result[i]),
headers: { "Content-Type": "application/json" }
};
client.post("http://192.168.1.102:8080/api/ToterFeed/v1", args, function (data, response) {
responseArray.push(data.responses);
console.log(data.responses[0]);
});
}
})
console.log("done");
This is the updated code based on my interpretation. I receive the "then is not a function" error.
var Promise = require('bluebird');
var Converter = Promise.promisifyAll(require("csvtojson")).Converter;
var converter = new Converter({});
var Client = Promise.promisifyAll(require('node-rest-client')).Client;
var client = new Client();
var responseArray = [];
converter.fromFile("./ToterFeed.csv").then(function(err,result){
for (var i = 0, len = result.length; i < len; i++) {
var args = {
data: JSON.stringify(result[i]),
headers: { "Content-Type": "application/json" }
};
client.post("http://192.168.1.102:8080/api/ToterFeed/v1", args, function (data, response) {
responseArray.push(data.responses);
console.log(data.responses[0]);
});
}
}).then(console.log("done"));
My goal is to run the fill read and array iteration to pletion then print "done". Any insights is much appreciated! Thanks.
I'm new to Node and this is the first time I've tried to use a promise library. With that said ,this is the simple example I'm following: http://zpalexander./blog/javascript-promises-node-js/
This is the code that executes without error however as expected it logs "done" prior to finishing the file read and looping through the array.
var Promise = require('bluebird');
var Converter = Promise.promisifyAll(require("csvtojson")).Converter;
var converter = new Converter({});
var Client = Promise.promisifyAll(require('node-rest-client')).Client;
var client = new Client();
var responseArray = [];
converter.fromFile("./ToterFeed.csv", function(err,result){
for (var i = 0, len = result.length; i < len; i++) {
var args = {
data: JSON.stringify(result[i]),
headers: { "Content-Type": "application/json" }
};
client.post("http://192.168.1.102:8080/api/ToterFeed/v1", args, function (data, response) {
responseArray.push(data.responses);
console.log(data.responses[0]);
});
}
})
console.log("done");
This is the updated code based on my interpretation. I receive the "then is not a function" error.
var Promise = require('bluebird');
var Converter = Promise.promisifyAll(require("csvtojson")).Converter;
var converter = new Converter({});
var Client = Promise.promisifyAll(require('node-rest-client')).Client;
var client = new Client();
var responseArray = [];
converter.fromFile("./ToterFeed.csv").then(function(err,result){
for (var i = 0, len = result.length; i < len; i++) {
var args = {
data: JSON.stringify(result[i]),
headers: { "Content-Type": "application/json" }
};
client.post("http://192.168.1.102:8080/api/ToterFeed/v1", args, function (data, response) {
responseArray.push(data.responses);
console.log(data.responses[0]);
});
}
}).then(console.log("done"));
My goal is to run the fill read and array iteration to pletion then print "done". Any insights is much appreciated! Thanks.
Share Improve this question asked Mar 29, 2016 at 16:20 KenKen 2252 gold badges5 silver badges12 bronze badges 2-
Bluebird doesn't change the methods of the promisified APIs, it extends them by methods with an
…Async
suffix that are the promise-returning ones. – Bergi Commented Mar 29, 2016 at 16:26 -
You need to pass a callback to
then
, not the result of the callconsole.log(…)
– Bergi Commented Mar 29, 2016 at 16:27
1 Answer
Reset to default 4You're almost there, a few small changes needed - the HTTP post will also be asynchronous, so as you've wrapped it using promisifyAll, you can return that from your first callback (which will mean subsequent calls to .then will await its pletion). Note also that promisifyAll creates extra functions with a suffix of Async, so you need to call those versions to get promises back.
As you're iterating over an array and creating multiple posts, you need to use Promise.all to await the pletion of all of the promises you're creating.
Lastly, your final console.log
needs to be wrapped in a function (.then()
always takes a function as a parameter). Something like this should work:
var Promise = require('bluebird');
var Converter = Promise.promisifyAll(require("csvtojson")).Converter;
var converter = new Converter({});
var Client = Promise.promisifyAll(require('node-rest-client').Client);
var responseArray = [];
converter.fromFileAsync("./ToterFeed.csv").then(function(result){
var promises = [];
for (var i = 0, len = result.length; i < len; i++) {
var args = {
data: JSON.stringify(result[i]),
headers: { "Content-Type": "application/json" }
};
// Add a promise to the array of promises - this
// will call the post then process the result.
promises.push(
Client.postAsync(
"http://192.168.1.102:8080/api/ToterFeed/v1",
args
).then(function (data) {
responseArray.push(data.responses);
}));
}
// Return Promise.all on all of our posts and
// their response handlers
return Promise.all(promises);
}).then(function () {
console.log("done");
});