简体   繁体   中英

What does "function(err, result)" in JavaScript

I don't understand what does this code do in JavaScript . Why it is used? As far as I know it is "callback" but how the data assigned to "result" ? I've never seen an information explains that.

 var prompt = require('prompt');
 prompt.start();
 prompt.get(['name'], function (err, result) {
 console.log('  name: ' + result.name);
 });

When you run a function that uses the callback, all you're doing is giving it another function for it to run when it is done. In your case, you are giving the prompt.get function the "function (err, result) {...}" function. When prompt.get is done and wants to return some result, it will call this function that you gave it. The function, as you can see, has parameters that can be passed to it (in your case, err and result). When it runs the callback (just like running any other function), it will give it parameters that are then exposed to the callback function you gave it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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