Understanding Callback function - javascript

i have trouble with the following code:
var placeingOrders = function(orderNumber) {
console.log("Order number: ", orderNumber);
requestedOrder(orderNumber, returnOrder);
};
function requestedOrder(orderNumber, callback) {
setTimeout(orderNumber, callback, 5000);
}
function returnOrder() {
console.log("Order number: " , " is delivered");
}
im trying to pass arguments on callback function but when i do as above i got the following error code:
timers.js:348
throw new TypeError('"callback" argument must be a function');
^
TypeError: "callback" argument must be a function
at exports.setTimeout (timers.js:348:11)
And of course if i run the same code without arguments it will work.
var placeingOrders = function(orderNumber) {
console.log("Order number: ", orderNumber);
requestedOrder(returnOrder);
};
function requestedOrder(callback) {
setTimeout(callback, 5000);
}
function returnOrder() {
console.log("Order number: " , orderNumber , " is delivered");
}
I would like to know what im exactly doing wrong here. How do i use this callback function correctly if i want to pass arguments.
(ps: im not a native english speaker, sry for that)

Why you get this error ?
setTimeout expects to get the callback function as the first argument, but at the first place you pass a number, so why the exception. The second case works, because you remove the orderNumber from the first place, an the function gets it.
You need to pass the arguments of the callback function after the 2nd place. See the setTimeout function signature. First goes the callback function, 2nd - the time, at least to call the function and after it everything which are passed goes to the callback function as arguments.
This is the signature of the function - [] are optional ones.
setTimeout(function[, delay, param1, param2, ...])
Code
var placeingOrders = function(orderNumber) {
console.log("Order number: ", orderNumber);
requestedOrder(orderNumber, returnOrder);
};
function requestedOrder(orderNumber, callback) {
setTimeout(callback, 5000, orderNumber);
}
function returnOrder(orderNumber) {
console.log("Order number: " + orderNumber + " is delivered");
}
placeingOrders(14);

Related

Callback function with optional second parameter

I have two functions which I send as callback function to the mapFunction method.
The problem is that the mapFunction method return callback with only one parameters declared inside of the function, and in both callbacks it has to be the first one.
func1(param) {...}
func2(param1, param2) {...}
mapFunction(list, callback) {
//....
const something = 'blabla';
// if callback has second param run callback(something, secondParam)
return callback(something)
}
mapFunction(list, func1);
mapFunction(list, func2); // send second param
**How do I send second parameter with the callback function and call function with two parameters if two exists, if not call just with the one definer inside the scope of the mapFunction. Is it possible ? **
You can use Function.length
function func1(param) { alert(param); }
function func2(param1, param2) { alert(param1 + ' ' + param2); }
function mapFunction(list, callback) {
const something = 'blabla';
console.log(callback.length);
return callback.length == 2 ? callback(something, list) : callback(something);
}
let list = 'secondParam';
mapFunction(list, func1);
mapFunction(list, func2);
JavaScript enables you to pass more arguments to a function than declared in its definition. So you can always pass two parameters to your callback and simply ignore the second parameter in func1
If I understand correctly, this code will help you. The first of values that must pass to func 1-3 is common between them and will pass from mapFunction, but other parameters will provide from where?? If you need pass them when calling mapFunc, you can try this stylish way that works fine with any other functions and any number of parameters (func4, ...):
function func1(p1) { console.log(p1); }
function func2(p1, p2) { console.log(p1 + ", " + p2); }
function func3(p1, p2, p3) { console.log(p1 + ", " + p2 + ", " + p3); }
function mapFunction(list, callback) {
const something = 'insideValue';
return callback.apply(callback, [something].concat(Array.apply(null, arguments).slice(2, 1+callback.length)));// you can remove 1+callback.length and pass all parameters to callback but ignore extra parameters there (in func1-3).
}
var list=["any thing ..."];
mapFunction(list, func1);
mapFunction(list, func2, "param2val");
mapFunction(list, func3, "param2val", "p3val");
Also you could have passed them as simple objects or array to the mapFunc as only one parameter.

return success data in require.js with callback

i have a little understanding problem with my current code...
i create a new require js module.
define(['jquery','apiomat'],function($,Apiomat) {
var getLocation = function(data, callback) {
Apiomat.Localization.getLocalizations("locale like 'de'", {
data: data,
onOk: function (data) {
callback(data);
}
});
};
return {
getData: function(data, callback) {
getLocation(data, callback);
}
}
});
And if i try to access these function with:
var test = easy.getData();
app.logger("CALLBACK FROM COMMON: " + JSON.stringify(test));
I always get this error message.
TypeError: callback is not a function. (In 'callback(data)', 'callback' is undefined)
Im not really sure what i have done wrong.
getData takes two arguments. The second one is supposed to be a function, but you aren't passing any arguments at all, so it gets the value undefined.
It then calls getLocation with the same arguments and Apiomat.Localization.getLocalizations does its thing. Eventually getLocalizations (or another function called by it) calls getOK which attempts to call callback.
undefined is not a function, so you get the error message.
Additionally the getData function doesn't have a return statement so will be returning undefined. This means there is no point in assigning the return value to test.
You need to pass a function which does whatever you want to do:
function myCallback(test) {
app.logger("CALLBACK FROM COMMON: " + JSON.stringify(test));
}
… and pass arguments to getData.
easy.getData("some data", myCallback);

Running functions in sync in node.js

I want to run func2 run only after func1 has finished.
{
func1();
func2();//
}
But when func1() starts, func2() does not wait for func1() to get finished. func2() run simultaneously with func1(), which causes run time error, because func2() need some input from func1().
I also tried using async module, but got no success.
Edit: I run the function check(), which call two functions prompt() and copyProject(). copyProject() should run only after prompt() function has finished, but it start executing simultaneously.
var check = function check (){
async.series([
function(callback){
prompt();
callback(null);
},
function(callback){
copyProject();
callback(null);
}
]);
};
var prompt = function prompt(){
var prompts = [{
name: "projectName",
message: "What is the name of your project?"
},{
name: "authorName",
message: "What is your name?",
}];
inquirer.prompt(prompts, function( answers ) {
for (var key in answers) {
if (answers.hasOwnProperty(key)) {
console.log(key + " -> " + answers[key]);
infoToRender[key] = answers[key]
}
}
});
};
var copyProject = function copyProject (){
// code to copy some files from one location to another based on input from prompt function
};
Your function prompt is itself an asynchronous function: The call to inquirer.prompts causes the function to wait and this makes it asynchronous. Since your prompt function does not use a callback, the async module can not know when prompt has finished: In contrast, it runs prompt and this function immediately returns. Hence async right afterwards runs copyProject.
What you need to do to solve this issue is to introduce a callback function in prompt that async "waits" for. So basically something like this:
var prompt = function (callback) {
// Do your stuff here...
inquirer.prompt(prompts, function (answers) {
// Your for loop here...
callback(null);
});
};
Then, within async you need to do:
async.series([
function (callback) {
prompt(callback);
},
function (callback) {
copyProject();
callback(null);
}
]);
Then copyProject is only run when prompt has been completed. Please note that async.series will not wait for copyProject to finish, in case this is an asynchronous function, as again, it does not take a callback. So same pattern applies here.

node async waterfall using an array with callbacks that have arguments

I am getting an error that I do not understand. I am calling async.waterfall with an array of functions. The function is 'shortened' for clarity.
FabricCommand.prototype.do = function (callback, undoArray) {
var self = this;
if (undoArray === undefined) {
undoArray = [];
}
undoArray.push(self);
callback(null, undoArray);
};
I create the array as listed below: doCommands is an array and the objects are added as such:
doCommands.push(fabricCommand.do.bind(fabricCommand));
the waterfall setup:
async.waterfall(
doCommands,
function(err, undoCommands){
if (err) {
// do something ...
}
else {
console.log('we succeeded with all the do commands... and there are '
+ undoCommands.length
+ ' in the undoCommands but we will disregard it...');
}
}
);
Now when I run this code, the first time through the FabricCommand.do function, I allocate the undoCommands array and I add one to it, next time through I get, where I try to add the array element, the following error:
undoArray.push(something);
^ TypeError: Object function (err) {
if (err) {
callback.apply(null, arguments);
callback = function () {};
}
else {
var args = Array.prototype.slice.call(arguments, 1);
var next = iterator.next();
if (next) {
args.push(wrapIterator(next));
}
else {
args.push(callback);
}
async.setImmediate(function () {
iterator.apply(null, args);
});
}
} has no method 'push'
Can anyone see what I am doing wrong?
The function that is executed by async.waterfall must have the following signature:
function(arg, callback) { … }
or, with multiple arguments:
function(arg1, arg2, callback) { … }
In your case, you simply inverted the two parameters:
FabricCommand.prototype.do = function (callback, undoArray) { … }
callback received the value intended to be stored in undoArray, and undoArray received the value intended for the callback, i.e. a function: that's why you encountered this weird error (function […] has no method 'push').
You need to put the parameters in the correct order:
FabricCommand.prototype.do = function (undoArray, callback) { … }
A second issue is that the first function of the waterfall receives only one parameter: the callback (because there is no value to be received, as it is the first function of the waterfall). A solution is to check the number of arguments:
if (Array.prototype.slice.apply(arguments).length === 1) {
callback = undoArray;
undoArray = undefined;
}
Here is a working gist.

JavaScript: Passing parameters to a callback function

I'm trying to pass some parameter to a function used as callback, how can I do that?
This is my try:
function tryMe(param1, param2) {
alert(param1 + " and " + param2);
}
function callbackTester(callback, param1, param2) {
callback(param1, param2);
}
callbackTester(tryMe, "hello", "goodbye");
If you want something slightly more general, you can use the arguments variable like so:
function tryMe(param1, param2) {
alert(param1 + " and " + param2);
}
function callbackTester(callback) {
callback(arguments[1], arguments[2]);
}
callbackTester(tryMe, "hello", "goodbye");
But otherwise, your example works fine (arguments[0] can be used in place of callback in the tester)
This would also work:
// callback function
function tryMe(param1, param2) {
alert(param1 + " and " + param2);
}
// callback executer
function callbackTester(callback) {
callback();
}
// test function
callbackTester(function() {
tryMe("hello", "goodbye");
});
Another Scenario :
// callback function
function tryMe(param1, param2, param3) {
alert(param1 + " and " + param2 + " " + param3);
}
// callback executer
function callbackTester(callback) {
//this is the more obivous scenario as we use callback function
//only when we have some missing value
//get this data from ajax or compute
var extraParam = "this data was missing";
//call the callback when we have the data
callback(extraParam);
}
// test function
callbackTester(function(k) {
tryMe("hello", "goodbye", k);
});
Your question is unclear. If you're asking how you can do this in a simpler way, you should take a look at the ECMAScript 5th edition method .bind(), which is a member of Function.prototype. Using it, you can do something like this:
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
function callbackTester (callback) {
callback();
}
callbackTester(tryMe.bind(null, "hello", "goodbye"));
You can also use the following code, which adds the method if it isn't available in the current browser:
// From Prototype.js
if (!Function.prototype.bind) { // check if native implementation available
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
}
Example
bind() - PrototypeJS Documentation
If you are not sure how many parameters are you going to be passed into callback functions, use apply function.
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
function callbackTester(callback,params){
callback.apply(this,params);
}
callbackTester(tryMe,['hello','goodbye']);
When you have a callback that will be called by something other than your code with a specific number of params and you want to pass in additional params you can pass a wrapper function as the callback and inside the wrapper pass the additional param(s).
function login(accessedViaPopup) {
//pass FB.login a call back function wrapper that will accept the
//response param and then call my "real" callback with the additional param
FB.login(function(response){
fb_login_callback(response,accessedViaPopup);
});
}
//handles respone from fb login call
function fb_login_callback(response, accessedViaPopup) {
//do stuff
}
Wrap the 'child' function(s) being passed as/with arguments within function wrappers to prevent them being evaluated when the 'parent' function is called.
function outcome(){
return false;
}
function process(callbackSuccess, callbackFailure){
if ( outcome() )
callbackSuccess();
else
callbackFailure();
}
process(function(){alert("OKAY");},function(){alert("OOPS");})
Code from a question with any number of parameters and a callback context:
function SomeFunction(name) {
this.name = name;
}
function tryMe(param1, param2) {
console.log(this.name + ": " + param1 + " and " + param2);
}
function tryMeMore(param1, param2, param3) {
console.log(this.name + ": " + param1 + " and " + param2 + " and even " + param3);
}
function callbackTester(callback, callbackContext) {
callback.apply(callbackContext, Array.prototype.splice.call(arguments, 2));
}
callbackTester(tryMe, new SomeFunction("context1"), "hello", "goodbye");
callbackTester(tryMeMore, new SomeFunction("context2"), "hello", "goodbye", "hasta la vista");
// context1: hello and goodbye
// context2: hello and goodbye and even hasta la vista
Use curried function as in this simple example.
const BTN = document.querySelector('button')
const RES = document.querySelector('p')
const changeText = newText => () => {
RES.textContent = newText
}
BTN.addEventListener('click', changeText('Clicked!'))
<button>ClickMe</button>
<p>Not clicked<p>
Faced this recently, to get it(especially if the parent function has multiple arguments doing different stuffs not related to the callback , is to have the callback placed with its argument in an arrow function passed as an argument.
function tryMe(param1, param2) {
alert(param1 + " and " + param2);
}
function callbackTester(callback, someArg, AnotherArg) {
callback();
}
callbackTester(()=> tryMe("hello", "goodbye"), "someArg", "AnotherArg");
...or simply if you dont have multiple arguments doing other stuff.
function tryMe(param1, param2) {
alert(param1 + " and " + param2);
}
function callbackTester(callback) {
callback();
}
callbackTester(()=> tryMe("hello", "goodbye"));
A new version for the scenario where the callback will be called by some other function, not your own code, and you want to add additional parameters.
For example, let's pretend that you have a lot of nested calls with success and error callbacks. I will use angular promises for this example but any javascript code with callbacks would be the same for the purpose.
someObject.doSomething(param1, function(result1) {
console.log("Got result from doSomething: " + result1);
result.doSomethingElse(param2, function(result2) {
console.log("Got result from doSomethingElse: " + result2);
}, function(error2) {
console.log("Got error from doSomethingElse: " + error2);
});
}, function(error1) {
console.log("Got error from doSomething: " + error1);
});
Now you may want to unclutter your code by defining a function to log errors, keeping the origin of the error for debugging purposes. This is how you would proceed to refactor your code:
someObject.doSomething(param1, function (result1) {
console.log("Got result from doSomething: " + result1);
result.doSomethingElse(param2, function (result2) {
console.log("Got result from doSomethingElse: " + result2);
}, handleError.bind(null, "doSomethingElse"));
}, handleError.bind(null, "doSomething"));
/*
* Log errors, capturing the error of a callback and prepending an id
*/
var handleError = function (id, error) {
var id = id || "";
console.log("Got error from " + id + ": " + error);
};
The calling function will still add the error parameter after your callback function parameters.
Let me give you a very plain Node.js style example of using a callback:
/**
* Function expects these arguments:
* 2 numbers and a callback function(err, result)
*/
var myTest = function(arg1, arg2, callback) {
if (typeof arg1 !== "number") {
return callback('Arg 1 is not a number!', null); // Args: 1)Error, 2)No result
}
if (typeof arg2 !== "number") {
return callback('Arg 2 is not a number!', null); // Args: 1)Error, 2)No result
}
if (arg1 === arg2) {
// Do somethign complex here..
callback(null, 'Actions ended, arg1 was equal to arg2'); // Args: 1)No error, 2)Result
} else if (arg1 > arg2) {
// Do somethign complex here..
callback(null, 'Actions ended, arg1 was > from arg2'); // Args: 1)No error, 2)Result
} else {
// Do somethign else complex here..
callback(null, 'Actions ended, arg1 was < from arg2'); // Args: 1)No error, 2)Result
}
};
/**
* Call it this way:
* Third argument is an anonymous function with 2 args for error and result
*/
myTest(3, 6, function(err, result) {
var resultElement = document.getElementById("my_result");
if (err) {
resultElement.innerHTML = 'Error! ' + err;
resultElement.style.color = "red";
//throw err; // if you want
} else {
resultElement.innerHTML = 'Result: ' + result;
resultElement.style.color = "green";
}
});
and the HTML that will render the result:
<div id="my_result">
Result will come here!
</div>
You can play with it here: https://jsfiddle.net/q8gnvcts/ - for example try to pass string instead of number: myTest('some string', 6, function(err, result).. and see the result.
I hope this example helps because it represents the very basic idea of callback functions.
function tryMe(param1, param2) {
console.log(param1 + " and " + param2);
}
function tryMe2(param1) {
console.log(param1);
}
function callbackTester(callback, ...params) {
callback(...params);
}
callbackTester(tryMe, "hello", "goodbye");
callbackTester(tryMe2, "hello");
read more about the spread syntax
I'm trying to pass some parameter to a function used as callback, how can I do that?
I think he is implying that he wants to call the function this callbackTester(tryMe, "hello", "goodbye"). To do this we can use the Rest Operator (...). This operator takes the arguments that a function receives and dumps them into a real array that we will use to access in our callback function.
Now, some other developers might also argue that we could use the arguments "array". That will be fine, but we should be careful with it. arguments is not a real array but an array-like object with a length property.
Here is a working snippet using the Rest Operator:
function tryMe(params) {
console.log(params.join(', '));
}
function callbackTester(callback, ...params) {
callback(params);
}
callbackTester(tryMe, 'hello', 'goodbye', 'hi again');
callbackTester(tryMe, 'hello', 'goodbye');
callbackTester(tryMe, 'hello');
Just use the bind() function which is primarily used to set the this value. However, we can also use it to pass parameters without calling the function due to bind() returning a new function with the sequence of arguments provided.
Example:
function foo(param1, param2, param3) {
console.log(param1, param2, param3);
}
setTimeout(foo.bind(null, 'foo', 'bar', 'baz'), 1000);
In the snippet above, the setTimeout function takes 2 arguments, the callback function and a minimum time in ms for the function to be called, so when passing the callback function we're going to use bind and specify the parameters
Note: The first parameter of bind is the value that we want to set for this, and because we're not interested on that, null was passed, the subsequent parameters in bind are going to be the parameters for the callback.
I was looking for the same thing and end up with the solution and here it's a simple example if anybody wants to go through this.
var FA = function(data){
console.log("IN A:"+data)
FC(data,"LastName");
};
var FC = function(data,d2){
console.log("IN C:"+data,d2)
};
var FB = function(data){
console.log("IN B:"+data);
FA(data)
};
FB('FirstName')
Also posted on the other question here
//Suppose function not taking any parameter means just add the GetAlterConfirmation(function(result) {});
GetAlterConfirmation('test','messageText',function(result) {
alert(result);
}); //Function into document load or any other click event.
function GetAlterConfirmation(titleText, messageText, _callback){
bootbox.confirm({
title: titleText,
message: messageText,
buttons: {
cancel: {
label: '<i class="fa fa-times"></i> Cancel'
},
confirm: {
label: '<i class="fa fa-check"></i> Confirm'
}
},
callback: function (result) {
return _callback(result);
}
});

Categories