2

I have this simple code :

console.log('calling doWork func...')
doWork('a', 'b', myCb);

function doWork(param1, param2, callback)
{
        console.log('in func')
        callback();
}


function myCb(a)
{
        console.log('in callback... for value ' + a)
}

I'm running the function with a callback function -

The output I get is :

"calling doWork func..."
"in func"
"in callback... for value undefined"    // notice the undefined

All ok.

Now, I want the callback to be called with a specified param - something like ( which is incorrect since the () is executing the func) :

doWork('a', 'b', myCb('xxxx'));

I want the output to be :

in callback... for value  xxxx

How do I send a parameter with the myCb call in doWork('a', 'b', myCb);? ( 'when you run the callback function , please run it according to value xxxx')

any help ?

p.s. I want to avoid any global flags solution

thanks.

5
  • may be duplicated stackoverflow.com/questions/1997531/… Commented Jul 25, 2012 at 7:01
  • @mask8 please search the term .bind in your link , I don't see it. so I assume it is not duplicate ( different solutions) Commented Jul 25, 2012 at 7:02
  • how about this one? stackoverflow.com/questions/3458553/… there are too much of this questions anyway Commented Jul 25, 2012 at 7:07
  • @mask8 Ok , Agree. ( didnt find it before though) ...:) Commented Jul 25, 2012 at 7:07
  • @RoyiNamir when that first question was written, ES5 had been approved less than a month prior. At that time, most browsers didn't support it, and most developers weren't familiar with it. The lack of .bind in the answers is definitely not suggestive that it is not a duplicate (and certainly not conclusive in any case). Commented Jul 25, 2012 at 7:10

4 Answers 4

4

The common solution is to create another function in place.

doWork('a', 'b', function () {
    myCb('xxxx');
});

You can also use a function that would abstract the currying away. JavaScript (ES5) even has one built-in – Function.prototype.bind. Mind you, the native bind will make your callback slow and has limited support in browsers (see the MDN page).

doWork('a', 'b', myCb.bind(null, 'xxxx'));
Sign up to request clarification or add additional context in comments.

2 Comments

What are the issue(s) with the native bind?
@pst, while binding a context, it also allows call and apply to redefine it dynamically. Try to implement such bind, and you'll see. That makes each call to a bound function slow. jsperf
1

One of the solutions is to use .bind:

doWork('a', 'b', myCb.bind(null, 'xxx'));

Comments

1

Or you could use apply and arguments

console.log('calling doWork func...')
doWork('va', 'b', myCb);

function doWork(param1, param2, callback)
{
        console.log('in func')


        callback.apply(this,arguments);
}


function myCb(a)
{
        console.log('in callback... for value ' + a)
}

JSbin Example

1 Comment

oh i see, i mistook the a, from cb with the first argument, you want to pass to the cb function then this won't help you
0
console.log('calling doWork func...')
doWork (myCb, "a", "b");
function doWork (callback) {
console.log('in func')
callback (arguments[1], arguments[2]);
}
function myCb (param1, param2) {
console.log('in callback... for value ' + param1)
}

Hope this help you

Comments

Your Answer

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.