简体   繁体   English

Node.js回调

[英]Node.js callback

I can't seem to grasp the concept of a callback. 我似乎无法掌握回调的概念。 I haven't worked with them before so bear with me. 我没有和他们一起工作,所以请耐心等待。 To get my hands wet, I'm trying to login to twitter with zombie.js. 为了弄湿我的手,我正试着用zombie.js登录twitter。

Here is an example: 这是一个例子:

var Browser = require("zombie");
var browser = new Browser({ debug: true})

browser.visit("https://mobile.twitter.com/session/new", function (callback) {
    browser.fill("username", "xxxxx");
    browser.fill("password", "xxxxx");
    browser.pressButton("Sign in", function (err, success) {
        if(err){
            console.log(browser.text('.message'));
            console.log('There has been a error: ' + err);
        }
        else{
            console.log('Worked!');
        }

    });
});

At the browser.pressButton part, it will determine if I have been able to successfully login or not, depending on if .message contains the text "Typing on your phone stinks, we know! Double-check your username and password and try again." 在browser.pressButton部分,它将确定我是否能够成功登录,具体取决于.message包含文本“在手机上打字很.message ,我们知道!请仔细检查您的用户名和密码,然后重试。 “

However, I don't understand how it determines to fire the callback err . 但是,我不明白它是如何决定触发回调err If .message isn't present in the html, then I would like to trigger the success callback to move onto the next function. 如果.message中没有.message ,那么我想触发成功回调以转到下一个函数。

The convention Zombie seems to use for callbacks comes from node.js where the first argument is an error object, which should be null on success, and any subsequent arguments are for the success case. Zombie似乎用于回调的约定来自node.js,其中第一个参数是一个错误对象,成功时应为null ,任何后续参数都是成功案例。 If you define a callback, the library you are using (Zombie in this case) will execute your callback function when their async operation is complete. 如果您定义了一个回调,那么您正在使用的库(在本例中为Zombie)将在异步操作完成时执行您的回调函数。 When your callback is invoked it means "OK, an operation has completed and you can now process the result as you see fit". 当你的回调被调用时,它意味着“好的,操作已经完成,你现在可以按照你的需要处理结果”。 Your code needs to look at that first argument to decide if the operation was a success or failure. 您的代码需要查看第一个参数以确定操作是成功还是失败。

When you accept a callback function as an argument and then perform some (possibly asynchronous) operation, the callback is the way for you to tell the calling library you are done, and again use that first argument to distinguish errors from success. 当您接受回调函数作为参数然后执行一些(可能是异步的)操作时,回调是您告诉调用库已完成的方式,并再次使用第一个参数来区分错误和成功。

Part of your confusion is probably coming from the fact that your function signature for the callback to browser.visit is wrong. 你混淆的部分原因可能是你对browser.visit回调的函数签名是错误的。 You need to name that first argument to clearly indicate it's an error like this: 你需要命名第一个参数,以清楚地表明它是这样的错误:

browser.visit("https://mobile.twitter.com/session/new", function (error, browser) {

So in the body of that anonymous callback function, if zombie couldn't load that page, the error argument will have info about the error. 所以在匿名回调函数的主体中,如果僵尸无法加载该页面,则error参数将包含有关错误的信息。 If the page did load correctly, error will be null and the browser 2nd argument can be used to further tell zombie to do more stuff on the page. 如果页面加载正确,则error将为null, browser第二个参数可用于进一步告诉zombie在页面上执行更多操作。 This is how Zombie says "I'm done with the visit operation, time for you to handle the results." 这就是Zombie所说的“我完成了visit操作,是时候处理结果了。”

visit doesn't pass a callback argument, the anonymous function you pass as an argument to visit IS THE CALLBACK . visit不传递一个回调参数,你传递的匿名函数作为参数来visit IS CALLBACK You could code it like this to clarify (although nobody does) 你可以这样编码来澄清(虽然没有人这样做)

browser.visit("https://mobile.twitter.com/session/new", function callback(error, browser) {

So that's a callback when a library needs to tell you it is done. 所以当图书馆需要告诉你它完成时,这是一个回调。 You don't invoke it. 你没有调用它。 The library invokes it and you put code inside it. 库调用它并将代码放入其中。

On the other hand, when your code does async operations, you need to invoke the callback that you received as a function argument appropriately to tell your caller that you are done and whether it was success for failure. 另一方面,当您的代码执行异步操作时,您需要适当地调用作为函数参数接收的回调,以告诉调用者您已完成以及失败是否成功。 In this case, you don't do any of your own async code, so there's no need for you to invoke any callback functions. 在这种情况下,您不会执行任何自己的异步代码,因此您无需调用任何回调函数。

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

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