1

I have the following code:

 exported.removeAllWatchesForUser = function (projectKey, userObj, done) {
   console.log('done='+done); // Function is recognized

    // Get list of all issues & remove the user from watchers list   
    exported.getIssueList(projectKey, function(err, response, done){
                console.log('done='+done); // callback is not recognize but is 'undefined

    });
};

My problem is that the callback function 'done' is recognized in line#2 but it is 'undefined' in line#6 inside the getIssueList callback.

How do i make it available in this function so that i can pass down the call back to successive async methods?

2 Answers 2

1

Remove done from the list of parameters:

function(err, response, done) ->  function(err, response)

Otherwise the parameter shadows the parameter with the same name of the outer function.

Sign up to request clarification or add additional context in comments.

Comments

1

You've redefined a separate done in the the second callback that will "hide" the value of the higher scoped done. If you either remove that done argument in the second callback or name it something different, then you can directly access the higher scoped done.

Here's your code with the second callback renamed to issueDone:

exported.removeAllWatchesForUser = function (projectKey, userObj, done) {
   console.log(typeof done); // Function is recognized

    // Get list of all issues & remove the user from watchers list   
    exported.getIssueList(projectKey, function(err, response, issueDone){
          // you can directly call done() here
    });
};

Or, if you aren't going to use issueDone, then you can just remove it from the declaration of the callback. Personally, if it is actually passed, I'd prefer to just give it a different name to acknowledge that it is present and available, but you can use either of these two options:

exported.removeAllWatchesForUser = function (projectKey, userObj, done) {
   console.log(typeof done); // Function is recognized

    // Get list of all issues & remove the user from watchers list   
    exported.getIssueList(projectKey, function(err, response){
          // you can directly call done() here
    });
};

When using inline callbacks like you are using, Javascript allows you to access all variables in your parent scopes as long as you don't redefine a new variable in the current scope with the same name as one in the higher scope. When you do redefine the variable (as either a local variable, function or named function argument), then it "hides" the value in the higher scope, essentially overriding it so you can't get to the higher scope.

So, the solution to accessing variables at higher levels of scope is to make sure you don't define a variable with the same name in the current scope.

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.