javascript - Node.js callback -


I can not seem to understand the concept of callback. I had not worked from them before I could bear with me to try to get my hand wet, I'm trying to log in with Twitter with zombie.

Here's an example:

  var requires browser = "zombie"; Var Browser = New Browser ({Debug: True}) Browser. Visit ("https://mobile.twitter.com/session/new", function (callback) {browser.fill ("user name", "XXXxx"); Browser.fill ("password", "xxxxx"); Browser. Press button ("sign in", function (mistake, success) {if (err) {console.log (browser.text ('. Message')) console .log ('an error has occurred:' + fault );} And {console.log ('done!');}});});   

On the browser. Button button part will determine whether I can not successfully login or if there is text in .message "Typing On Your Phone Smells, we know! Your username and password re-check and try again. "

However, I do not understand how this callback err does fire if .message html If I do not exist in, then I go to the next function to get success callback I want to trigger.

Conference first argument where it comes from node.js find use for zombie callback error object, which should be null on success, and the arguments after any case of success, if you define a callback, you are using the library (Zombie in this case) you The callback function will execute when their async operations are completed. When your callback is applied, it means "OK, one operation is complete and you can now process the result as you see".

When you accept as an argument to a callback function and some (possibly asynchronous) callback when the operation modes of assigning call library for you, and to distinguish errors from success Use logic for the first time.

Part of your confusion is coming from the fact that your function for callback is signature browser. Visit is incorrect. You need to give this first argument that it clearly indicates that this is an error:

  browser.visit ("https://mobile.twitter.com/session/new In the body of that anonymous callback function, if the zombie could not load that page, then the error in the  error  logic, the function (error, browser) {  

If the page is correctly loaded, then error will be zero and the browser is the second argument. The more stuff on Ej can be used to tell the Zombie. "I visits has been with the operation, the time to handle the results for you."

visit does not pass the callback logic, anonymous function as argument to visit callback . It can code (though none)

  Browser. Visit ("https://mobile.twitter.com/session/new" , Function callback (error, browser) {  

is a callback when a library requires you to be told, it is done. You do not apply it, the library invites it and you put the code inside it. On the other hand, when your code operates asynchronous, you need to call the callback that you have received as a function argument in order to tell the caller. And what was the success for failure? In this case, you do not have any of your async codes, so there is no need to invite Callback Functions.

Comments