HI Wasiu ,
The problem you are facing is the async behaviour of JavaScript.
when the function
citifmonlineDetail(citi,function(result) {
return result;
}) ;
is called the function directly returns undefined , since the function not
containing return statement.
There are two ways to solve
1) simply changing to
var data ;
citifmonlineDetail(citi,function(result) {
data = result;
console.log(data);
}) ;
2) BY using promise
var promise = new Promise(function(resolve) {
// do a thing, possibly async, then…
function hello(){
setTimeout(function(){
debugger;
resolve("hello");
},1000)
};
hello();
});
promise.then(function(val){
data = val;
console.log(data);
});
On Wednesday, 2 December 2015 19:38:21 UTC+5:30, wasiu razak wrote:
>
> am trying to grab data from a function , by passing in a args and return
> data
> but when i run i get undefined before i get the data , i have wrapped it
> in a time still same output
> version 2
>
> function citifmonlineDetail(url, callback) {
>
> setTimeout(function() {
> request(url, function(error, response, html) {
>
> var detail = "";
>
> if (!error) {
> $ = cheerio.load(html);
>
> $('.entry p').each(function(index, paragraphs) {
> console.log($(paragraphs).text());
> var a = $(paragraphs).text();
> detail = detail.concat(a + " ");
> });
>
> callback(detail);
> }
>
>
> });
> }, 3000);
> }
> var citi = '
> http://citifmonline.com/2015/12/01/no-bias-in-disbursing-leap-funds-gender-minstry-insists/
> ';
>
> var data= citifmonlineDetail(citi,function(result) {
> return result;
> }) ;
> console.log(data);
>
>
> version 1
>
> function citifmonlineDetail(url) {
> request(url, function(error, response, html) {
> if (!error) {
> $ = cheerio.load(html);
> var b;
>
> $('.entry p').each(function(index, paragraphs) {
> var a =$(paragraphs).text();
> b =b.concat(a);
>
> });
> return b
> }});
>
> }
>
>
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/02514b9f-a0f4-47c3-b1d6-f06b02a3ab82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.