I'm new to Node/Javascript and trying to grab a url audioLink by loading another url songLink. I would then add them to a new object rapSong. To achieve this I am using async series so that the calls will be happen asynchronously. I have tried other methods such as using async waterfall and just callbacks with no success.
audioLink is undefined unless I am within the getAudioLink function. Also the functions in the async.series do not run in the correct order. Any advice?
//for each song..
songLinkElem.each(function(i, s) {
var songName = StringUtils.removeWhiteSpacesAndNewLines($(s).children(".title_with_artists").text());
var songLink = $(s).attr("href");
var audioLink;
async.series([
function(callback){
function getAudioLink(songLink, callback){
request(songLink, function(err,resp,body){
$ = cheerio.load(body);
var audioElem = $(body).find(".audio_link > a");
var audioLink = audioElem.attr("href");
return callback(audioLink);
});
}
getAudioLink(songLink, function(resp){
audioLink = resp;
console.log("one");
})
console.log("two");
callback();
},
function(callback){
var rapSong = new Song(songName, artistLink, songLink, audioLink);
rapArtist.addSong(rapSong);
console.log("three");
callback();
}],
function(err, result){
console.log("four");
});
Within your first async.series function, you shouldn't call its callback until the callback for getAudioLink is called.
So move the code that follows the getAudioLink call inside its callback:
getAudioLink(songLink, function(resp){
audioLink = resp;
console.log("one");
console.log("two");
callback();
});
That way the async.series won't proceed until after audioLink is set.
Related
I've inherited some Node.js code and I need to add some functionality. However, I'm not sure syntactically how to accomplish my goal due to the asynchronous nature of Node. Currently, I have a function defined like this:
return {
myEntryPoint: function(req, res) {
var results = getResults();
res.send(200, results);
}
};
Several pieces are calling this function already. Inside of it, I'm calling a function called getResults which is defined like this:
var getResults = function() {
var results = [];
async.series([
function(callback) {
// add to results
},
function(callback) {
// add to results
}
]);
return results;
};
My problem is, I need to wait until all of the functions inside of the async.series call are made before I return the results. How do I do this?
You could change it to add a callback. Series has an optional callback to run once all the functions have completed within.
var getResults = function(finished) {
var results = [];
async.series([
function(callback) {
// add to results
},
function(callback) {
// add to results
}
], function() {
//Called when series is finished
finished(results);
});
};
And to get the results,
return {
myEntryPoint: function(req, res) {
getResults(function(results) {
res.send(200, results);
});
}
};
The usual way to handle this is to take a callback that uses the argument in question.
results = getResults(args);
someFunction(results);
Is equivalent to
function getResults(args, callback) {
var results;
//do stuff and populate results.
callback(results);
}
getResults(args, someFunction);
Except you don't have the issue of trying to wait for async stuff to happen, which you can't do, without weird logic.
when I use deferred.resolve way to promise a action,I can't get the content of the file
function readFile(fileName) {
var deferred = Q.defer();
fs.readFile(fileName, 'utf-8', deferred.resolve);
return deferred.promise;
};
readFile('test.txt').then(function (err, data) {
console.log('data:' + data)
})
I get data:undefined output
but it works OK fine when I promised action httpGet
var httpGet = function (opts) {
var deferred = Q.defer();
http.get(opts, deferred.resolve);
return deferred.promise;
};
httpGet('http://www.google.com').then(function (res) {
console.log("Got response: " + res.statusCode);
res.on('data', function (data) {
console.log(data.toString());
})
}
);
Is there something wrong the code above and in this way how can i get the content of the file.
or is there something different between fs.readFile and http.get?
You can use Q.nfcall to call a NodeJS function promisified.
function httpGet(opts){
return Q.nfcall(http.get,opts);
}
Or simply:
var httpGet = Q.nfbind(http.get,http)
This would also work for fs.readFile by the way.
If you want to do it manually. Q's deferred objects have a .makeNodeResolver function which lets you pass them around safely like you do:
var httpGet = function (opts) {
var deferred = Q.defer();
http.get(opts, deferred.makeNodeResolver());
return deferred.promise;
};
One of the things .makeNodeResolver does is bind the .this value.
It is better to use .nfcall though.
I had a previous question Object [object Object] has no method 'test' Now the issue with object is gone but the call back is not working the console.log prints undefined
this.test = function(callback) {
callback('i am test');
};
var self = this;
module.exports.config = function (settings, callback) {
self.test(function(err,res){
console.log(res);
});
};
Please ignore as i am new to nodejs
this.test() expects a callback that will be passed a string as the first argument. You are giving it a callback function that uses the second argument, not the first argument. Since no second argument is passed to the callback, you get undefined for it.
Change your call from this:
self.test(function(err,res){
console.log(res);
});
to this:
self.test(function(err){
console.log(err);
});
Or, alternatively change the implementation of this.test() to pass a second argument to its callback.
this.test = function(callback) {
// pass two arguments to the callback
callback('i am test', 'this is me');
};
var self = this;
module.exports.config = function (settings, callback) {
self.test(function(err,res){
// now both arguments err and res are available here
console.log(res);
});
};
I have this async function that I want to turn into a promise
var myAsyncFunction = function(err, result) {
if (err)
console.log("We got an error");
console.log("Success");
};
myAsyncFunction().then(function () { console.log("promise is working"); });
and I get TypeError: Cannot call method 'then' of undefined.
What is wrong with this code?
There are various ways in Q:
Q.nfcall(myAsyncFunction, arg1, arg2);
Q.nfapply(myAsyncFunction, [arg1, arg2]);
// Work with rusable wrapper
var myAsyncPromiseFunction = Q.denodeify(myAsyncFunction);
myAsyncPromiseFunction(arg1, arg2);
in Deferred implementation:
var myAsyncPromiseFunction = deferred.promisify(myAsyncFunction);
myAsyncPromiseFunction(arg1, arg2);
One notable difference: Wrappers as generated by Deferred additionally auto-resolve promises passed as an arguments, so you can do:
var readFile = deferred.promisify(fs.readFile);
var writeFile = deferred.promisify(fs.writeFile);
// Copy file
writeFile('filename.copy.txt', readFile('filename.txt'));
myAsyncFunction return nothing(undefined actually) in your code.
If you use whenjs, the normal way will be like this:
var myAsyncFunction = function() {
var d = when.defer();
//!!!do something to get the err and result
if (err)
d.reject(err);
else
d.resolve.(result);
//return a promise, so you can call .then
return d.promise;
};
Now you can call:
myAsyncFunction().then(function(result(){}, function(err){});
Using caolan's async library for node.js, I've been trying to call a function that uses async.series inside another function that uses async.series, but I still can't get the functions to run in the correct order, as detailed below:
The terminal output shows the second function being called before the first, for no apparent reason:
The "sys" module is now called "util". It should have a similar interface.
Starting the second step in the series
Value of b: undefined
Invoking the function firstStep
the value of toObtain is: [object Object]
And here's the corresponding source code:
var im = require('imagemagick');
var async = require('async');
var toObtain;
var b;
async.series([
function (callback) {
//It appears that this function is being invoked after the second function.
//Why is this happening?
firstStep();
callback();
},
function (callback) {
//Why is the output of this function being displayed BEFORE the output of the function above? It's the opposite of the order in which I'm calling the functions.
console.log("Starting the second step in the series");
console.log("Value of b: " + b);
}]);
function firstStep(){
async.series([
function (next) { // step one - call the function that sets toObtain
im.identify('kittens.png', function (err, features) {
if (err) throw err;
console.log("Invoking the function firstStep");
toObtain = features;
//console.log(toObtain);
b = toObtain.height;
next(); // invoke the callback provided by async
});
},
function (next) { // step two - display it
console.log('the value of toObtain is: %s',toObtain.toString());
}]);
}
After about an hour of experimentation, I got it to work properly. I simply modified the firstStep function so that it takes a callback function as a parameter, and calls the callback at the end of the firstStep function.
var im = require('imagemagick');
var async = require('async');
var toObtain = false;
var b;
async.series([
function (callback) {
firstStep(callback); //the firstStep function takes a callback parameter and calls the callback when it finishes running. Now everything seems to be working as intended.
},
function (callback) {
console.log("Starting the second step in the series");
console.log("Value of b: " + b);
}]);
function firstStep(theCallback){
async.series([
function (next) { // step one - call the function that sets toObtain
im.identify('kittens.png', function (err, features) {
if (err) throw err;
console.log("Invoking the function firstStep");
toObtain = features;
//console.log(toObtain);
b = toObtain.height;
next(); // invoke the callback provided by async
});
},
function (next) { // step two - display it
console.log('the value of toObtain is: %s',toObtain.toString());
theCallback();
}]);
}