Reputation:
I am starting with nodejs and read basics on callback, here i have a code,
exports.handler = (event, context, callback) => {
var bx = require('barcode-js');
// Set default values.
var params = {
input: './1.bmp',
type: 'qrcode'
};
bx.analyze(params, function (err, results) {
if (err) {
callback('There was an error processing this image: ' + err)
}
results.forEach(function(result) {
callback(null,'Result type :' + result.type + '\t'+'Value: '+result.value);
});
});
};
what is happening bx.analyze(params, function (err, results) {
in this line. why can't we just use bx.analyze(params) ?
Upvotes: 2
Views: 6258
Reputation: 12449
The second parameter is function
. Called a callback
.
When you execute an async function, you can't wait for its returned value:
var result = bx.analyze(params); //you can't do this
So you tell the function that when it finishes its job, just call the callback
(the function you passed as second parameter).
//so `analyze()` will call your passed function rather than `return` the result
//`err` will contain the error object if anything wrong happened in the analyze() function
//`results` will contain result if everything was fine
bx.analyze(params, function (err, results) {
if (err)
callback('There was an error processing this image: ' + err)
});
I strongly recommend you to learn how async code works in javascript. You can't learn Nodejs untill then..
Update:
In your code, the function handler
is an async function so it takes a callback
as a parameter (Just like analyze()
).
When this function done with its job, it calls the callback
.
Now in your code, it is called 2 times:
1) When an error occured, this function will call the callback and pass the error into it:
if (err) {
callback('There was an error processing this image: ' + err); //its passing "err" returned from analyze()
}
2) When everything goes well, it passes result set:
callback(null, 'Result type :' + result.type + '\t'+'Value: '+result.value); //the 1st parameter is being passed as "null" because there is no error in it, and second parameter passes the result
Upvotes: 2
Reputation: 2375
if we use bx.analyze(params)
then it is blocking (synchronous) code. event loop stop until bx.analyze(params)
did not return any value and
there is no error handling.
if we use bx.analyze(params, function (err, results) { });
then it is asynchronous (non-blocking) code. event loop will not wait for returning value it goes to next statement and when bx.analyze(params, function (err, results) {
return the callback event loop process it.
There is error handlation is also done
for deep understanding see this video https://www.youtube.com/watch?v=8aGhZQkoFbQ
Upvotes: 0
Reputation: 5192
This is callback function which make the code asynchronous.
Means you are passing a function as argument to your bx.analyze(params)
method. This callback method is executed once your bx.analyze(params)
is finished, so its not blocking the other code, making the code asynchronous.
If you need how this call back is executed , then you have to look for Event loop
, search it in Google,have lots of documentation.
Upvotes: 0