javascript - Function returning before async method done Node JS - Stack Overflow

I currently have a function that is called from my router:router.js:var result = Api.getUser();console.

I currently have a function that is called from my router:

router.js:

var result = Api.getUser();

console.log("Result: " + result);

api.js

exports.getUser = function(req, result) {

    request.get({
        uri: URL + '/user/me/',
        headers: {Authorization: 'bearer ' + req.user.accessToken},
        json: true
      }, function(e, r, body) {

        console.log("e: " + e + " body: %j", body);

            if(e) {

                return "{error: true}";

            } else {

                return body;

            }

    });

};

The problem I am having is that I am getting the log "Result: undefined" first.

Is there a way to hold the function getUser from returning until the function for get finishes?

I currently have a function that is called from my router:

router.js:

var result = Api.getUser();

console.log("Result: " + result);

api.js

exports.getUser = function(req, result) {

    request.get({
        uri: URL + '/user/me/',
        headers: {Authorization: 'bearer ' + req.user.accessToken},
        json: true
      }, function(e, r, body) {

        console.log("e: " + e + " body: %j", body);

            if(e) {

                return "{error: true}";

            } else {

                return body;

            }

    });

};

The problem I am having is that I am getting the log "Result: undefined" first.

Is there a way to hold the function getUser from returning until the function for get finishes?

Share Improve this question edited Jun 1, 2015 at 21:32 Dfranc3373 asked Jun 1, 2015 at 21:20 Dfranc3373Dfranc3373 2,1775 gold badges31 silver badges44 bronze badges 1
  • You're correct, changed in the question – Dfranc3373 Commented Jun 1, 2015 at 21:32
Add a ment  | 

3 Answers 3

Reset to default 5

Promises are awesome, I would suggest looking into them. However, a simple callback will do the trick

api:

exports.getUser = function(req, result, callback) {
  request.get({
    uri: URL + '/user/me/',
    headers: {Authorization: 'bearer ' + req.user.accessToken},
    json: true
  }, function(e, r, body) {
        if(e) {
            callback({error: true});
        } else {
            callback(body)
        }
    });
};

router:

var result
Api.getUser(req, result, function (response) {
  result = response
  console.log(result)
});

You are dealing with asynchronous code. There are a couple ways to solve this problem.

With A Promise

// api.js
var Promise = require('bluebird');
var request = require('request');
var getP = Promise.promisify(request.get.bind(request));

exports.getUser = function(accessToken) {
  return getP({
    uri: URL + '/user/me/',
    headers: {Authorization: 'bearer ' + accessToken},
    json: true
  }).spread(function(e, r, body) {
    console.log("e: " + e + " body: %j", body);
    if(e) {
      return Promise.reject("{error: true}");
    } else {
      return Promise.resolve(body);
    }
  });
};

// main.js
api.getUser(req.user.accessToken)
  .then(console.log.bind(console, "Result: "))
  .catch(console.error.bind(console));

With A Callback

// api.js
var request = require('request');

exports.getUser = function(accessToken, callback) {
  request.get({
    uri: URL + '/user/me/',
    headers: {Authorization: 'bearer ' + accessToken},
    json: true
  }, function(e, r, body) {
    console.log("e: " + e + " body: %j", body);
    if(e) {
      callback("{error: true}");
    } else {
      callback(null, body);
    }
  });
};

// main.js
api.getUser(req.user.accessToken, function (err, result) {
  if (err) return console.error(err);
  console.log("Result: " + result);
});

The nice thing about the promise API is that you only ever need to check for errors once, in your final .catch handler. In the callback style if you need to keep making additional asynchronous calls then you'll have to keep nesting callbacks and checking if (err) return console.error(err) in every single callback.

You can't return from an async function, you use a callback:

exports.getUser = function(req, result, callback) {
   request.get({
    uri: URL + '/user/me/',
    headers: {Authorization: 'bearer ' + req.user.accessToken},
    json: true
  }, function(e, r, body) {
        if(e) {
            callback({error: true});
        } else {
            callback(body);
        }
    });
};

Api.getUser(req, result, function(user) {
    console.log(user);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744042900a4548642.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信