Callback in NodeJS

Refer to:

http://tech.richardrodger.com/2011/04/21/node-js-%E2%80%93-how-to-write-a-for-loop-with-callbacks/

http://stackoverflow.com/questions/6789050/node-js-callback-not-working

Let’s also say you have a magical upload function that can do the upload:

upload('myfile.ext', function(err){

  if( err ) {

    console.log('yeah, that upload didn't work: '+err)

  }

})

This upload function uses the callback pattern you’d expect from Node. You give it the name of the file you want to upload and then it goes off and does its thing. After while, when it is finished, it calls you back, by calling your callback function. It passes in one argument, an err object. The Node convention is that the first parameter to a callback is an object that describes any errors that happened. If this object is null, then everything was OK. If not, then the object contains a description of the error. This could be a string, or a more complex object.

Node is asynchronous. The upload function will return before it even starts the upload.

你可能感兴趣的:(callback)