1

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)

asked Oct 15, 2017 at 12:05
1
  • requestedOrder(orderNumber, returnOrder); returnOrder is not defined and you dont set it to call the function requestedOrder Commented Oct 15, 2017 at 12:08

1 Answer 1

4

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);

answered Oct 15, 2017 at 12:08
Sign up to request clarification or add additional context in comments.

3 Comments

You will also want to give returnOrder a parameter and actually use it somewhere
The Params passed after delay are not always passed to the callback, using an anonymous self invoking function would be safer
are not always passed to the callback - can you clarify a bit?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.