用户定义的函数,在nodejs中具有回调 [英] user defined function with callback in nodejs

查看:104
本文介绍了用户定义的函数,在nodejs中具有回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个例子,我们在其中创建一个特定的函数,同时还具有一个回调函数?

can anyone give me an example in which we are creating a particular function, which is also having a callback function ?

function login(username, password, function(err,result){
});

我应该在哪里放置登录函数和回调函数的代码?
附注:我是NodeJS的新手

where should I put the code of the login function and callback function?
p.s.: I am new to nodejs

推荐答案

以下是登录功能的示例:

Here's an example of the login function:

function login(username, password, callback) {
    var info = {user: username, pwd: password};
    request.post({url: "https://www.adomain.com/login", formData: info}, function(err, response) {
        callback(err, response);
    });
}

并调用登录功能

login("bob", "wonderland", function(err, result)  {
    if (err) {
        // login did not succeed
    } else {
        // login successful
    }
});

这篇关于用户定义的函数,在nodejs中具有回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆