Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

javascript - Undefined return value from the function call Javascritpt

I'm trying to get the return value, but always get undefined.

        var hasNext;
         es.search(nextQuery, function (err, data) {
            if(data.hits.hits.length) {
              return hasNext = true;
            }
            return hasNext = false;
        });

I'm not sure how can I get whatever the return value and use it somewhere else? I need to use whatever this return value to validate with other functions, but it seems to be in the scope or something.

This is my code:

functions.getRecentPost = function ( req, res, next ) {
    ......... 
    // This will get all the post
es.search(query, function (err, data) {
    if(err) { // if error means no post and redirect them to create one
        return res.redirect('/new/post');
    }

              ....
    content.hasPrevious = hasPreviousPage(_page, content.posts.length); // this function is okay

    hasNextPage(_page, content.posts.length, function (data) {

            content.hasNext = data;

        });
        res.render("index.html", content);
});
};



function hasNextPage (pageNum, totalPost, callback) {

    es.search(query, function (err, data) {
        if(data.hits.hits.length) {
        return callback(true);
        }
        return callback(false);
    });
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Move the following line:

res.render("index.html", content);

into the hasNextPage callback:

functions.getRecentPost = function ( req, res, next ) {

  //.........

  es.search(query, function (err, data) {

    //.........

    hasNextPage(_page, content.posts.length, function (data) {

      content.hasNext = data;
      res.render("index.html", content);

    });
  });
};

If you expect getRecentPost to return something then you need to add a callback to it as well so that you can use it's return value. For example, if you expect to be doing this:

functions.getRecentPost = function ( req, res, next) {

  //......

  return content;
}

doSomething(functions.getRecentPost(x,y,z));

It won't work because the final value of content will be retrieved asynchronously. Instead you need to do this:

functions.getRecentPost = function ( req, res, next, callback ) {

  //.........

  hasNextPage(_page, content.posts.length, function (data) {

    content.hasNext = data;
    res.render("index.html", content);

    callback(content);

  });
};

functions.getRecentPost(x,y,z,function(content){
  doSomething(content);
})

You cannot return data asynchronously. You need to change your code (and your thinking) from writing stuff like this:

asyncFunction(function(data){
    foo = data;
});

doSomething(foo);

into this:

asyncFunction(function(data){
    doSomething(data);
});

Basically, move all the code that you would want to run after the async function into the callback function you passed into it.

Regular imperative code looks like this:

function fN () {
  x = fA();
  y = fB(x);
  z = fC(y);
  return fD(fE(z));
}

asynchronous code looks like this:

function fN (callback) {
  fA(function(x){
    fB(x,function(y){
      fC(y,function(z){
        fE(z,function(zz){
          fD(zz,function(zzz){
            callback(zzz);
          });
        });
      });
    });
  });
}

Notice that you don't return, you pass in a callback instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...