nodejs 异步函数不返回

nodejs async function not returning

本文关键字:返回 函数 异步 nodejs      更新时间:2023-09-26

我有一个函数。在这个函数中,我检查我验证了一个令牌,向Facebook发送了一个http请求,并向我的云服务器发送了另一个http请求。

function myAsyncFunction(token,fbat){
    if (token) {
        jwt.verify(token, jwt_secret, {
            issuer: 'me@me.com'
        }, function(err, decoded) {
            if (err) {
                if (err.name === 'TokenExpiredError') {
                    console.log('Token expired'); // renew token
                    var options = {
                        hostname: 'graph.facebook.com',
                        port: 443,
                        //path: '/oauth/?appsecret_proof='+hash+'&access_token='+at,
                        path: '/v2.5/me?fields=id&access_token=' + fbat,
                        method: 'GET'
                    }; //end of options

                    var callback = function(response) {
                        var str = '';
                        //another chunk of data has been received, so append it to `str`
                        response.on('data', function(chunk) {response on;
                            str += chunk;
                        });
                        response.on('end', function() { //start of response end;
                            var json = JSON.parse(str);
                            if (json.hasOwnProperty('id')) {
                                var params = {
                                    TableName: 'Users',
                                    Key: {
                                        'fid': {
                                            'N': json.id.toString()
                                        }
                                    },
                                    ConsistentRead: false,
                                    ProjectionExpression: 'fid,st'
                                };

                                dynamodb.getItem(params, function(err, data) {
                                    if (err) {
                                        return false;
                                    } else { //start of dynamodb else                                       
                                        if (isEmptyObject(data)) {
                                            return false;

                                        }
                                        else {
                                            if (data.Item.st.S === 't') {

                                               return true;
                                            } else {
                                              return false;
                                            }

                                        }

                                    }
                                });

                            }
                            else {
                                return false;
                            };

                        });
                        response.on('error', function() {
                            return false;
                        });
                    };
                    https.request(options, callback).end();
                } else if (err.name === 'JsonWebTokenError') {
                    return false;    
                } else {
                    return false;
                }
            } else {
                return true;   
            }
        });
    } else {
        return false;
    }
    return false;
};

然后我尝试调用此函数:

myAsyncFunction(token, fbat, function(result){
if(result){
//do some network calls here
}
else{
//do some other network calls here
}
});

当我调用异步函数时,我对其进行了调试,它正在调用Facebook和我的服务器,问题是它没有返回任何内容。它到达了返回语句的点,但检查结果是真还是假的条件永远不会被执行。

您的myAsyncFunction只接受两个参数:function myAsyncFunction(token,fbat) { ... } 。因此,您在调用时传入的回调永远无法执行。此外,异步函数中的各种return语句也不会产生预期的效果。

我建议阅读node的回调风格.js/javascript。

您最终要查找的是以下内容:

function myAsyncFunction(token, fbat, callback) {
    someOtherasyncFunction(arg1, arg2, function(err, result) {
         if (err) {
              return callback(err); // propagate error
         }
         yetAnotherAsyncFunction(argA, argB, function(err, result2) {
              if (err) {
                  return callback(err); // propagate error
              }
              // do something with result2...
              callback(null, myResult); // result2 is the result being passed into the callback
         });
    });
}

现在,当您调用函数时:

myAsyncFunction(token, fbat, function(err, result) {
    if (err) {
        // handle error... something has gone wrong...
    }
    // do something with the result...
});