简体   繁体   English

如何编写要在Java回调上使用或不使用回调的函数?

[英]How to code a function to be used with or without callback on Javascript?

I'm wondering how (if possible) I can create the same function, with exactly the same functionality, but to be used with a callback or without it. 我想知道如何(如果可能的话)创建具有完全相同功能的相同功能,但要与回调一起使用或不使用回调。 Here is an example with the "wanted effect" that is not working: 这是一个“无效效果”无效的示例:

function getUsers(req, res, onComplete) {
    // If the user is not logged in send an error. In other case, send data
    if (!req.session.session_id) {
        if (typeof onComplete === 'function') {
            onComplete({error: true, msg: 'You are not logged in'});
        } else {
            res.json({error: true, msg: 'You are not logged in'});
        }  
    } else {
        //Another similar code...
    }
}

It's not working because if I call "getUsers(req, res)", the typeof onComplete is always function, so I cannot detect when I call with or without the callback. 这是行不通的,因为如果我调用“ getUsers(req,res)”,则onComplete的类型始终是函数,因此我无法检测到何时使用或不使用回调。

The exact problem is that I can call this function from inside my code, with callback (a normal call, like getUsers(req, res, function(cb) {//Do something with cb}); OR I can call this function from an AJAX call in my website, like http://localhost:8080/api/getUsers , what in that case is when it don't works. 确切的问题是,我可以在代码内部通过回调(正常调用,例如getUsers(req, res, function(cb) {//Do something with cb});来调用此函数getUsers(req, res, function(cb) {//Do something with cb});或者我可以从我的网站中的AJAX调用,例如http://localhost:8080/api/getUsers ,在这种情况下是什么时候不起作用。

In the last case I get typeof onComplete === 'function' as true, so I never get the else part executed. 在最后一种情况下,我将typeof onComplete === 'function'设为true,因此我从未执行过else部分。 I'm supposing that the "request" done by the http call have more parameters than req&res, and that's the reason why onComplete is a function, instead of undefined. 我假设通过http调用完成的“请求”具有比req&res更多的参数,这就是为什么onComplete是一个函数而不是未定义的原因。

The usual AJAX call is like this (javascript in the client side): 通常的AJAX调用是这样的(客户端中的javascript):

function getAllUsers() {
    $.ajax({
        url: '/api/getUsers',
        type: 'GET',
        success: function(data) {
            // Remove item and set as main topic the assigned on the server 
            printUsers(data.users[0]);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
            alert(XMLHttpRequest.responseText); 
        } 
    });
}

And the route defined in the config.json of my Node.JS to call the final function is like that: 在Node.JS的config.json中定义的调用最终函数的路由是这样的:

{"path": "/api/getUsers", "method":"get", "callback": "api#getUsers"},

If you call getUsers without onComplete the value will be set to undefined. 如果调用不带onComplete的getUsers,则该值将设置为undefined。 You can then check that case in your function. 然后,您可以在函数中检查该情况。

function getUsers(req, res, onComplete) {
    // Set onComplete to default call back if it is undefined
    onComplete = onComplete || function(msg){ res.json(msg) };

    if (!req.session.session_id) {
        onComplete({error: true, msg: 'You are not logged in'});  
    } else {
        //Another similar code...
    }
}

See http://www.markhansen.co.nz/javascript-optional-parameters/ for more ways of doing this 有关更多操作方法,请参见http://www.markhansen.co.nz/javascript-optional-parameters/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM