2

Hi I've consulted: https://stackoverflow.com/a/9652434/3109607 but still have trouble with callbacks. I've also checked JS is sexy, callbackhell-dotcom, CW Beuchler's blog, the Maxogen repo on Github, and others on this topic.

Mr. Beuchler was kind enough to respond to my question but without him pointing it out I can't identify which function "thingToRun" is in the gist linked below. I'd like to include the code here but I had trouble so I had to use the gist.

In most functions this is written as "callback" so he was writing to illustrate that it doesn't have to be, but I still can't identify which function is being passed into getData. When I eliminate Mr. Beuchler's comments and try to find it on my own I have trouble. To me, it looks like logData (I'm just basing that on the arguments each function receives) but that's a regular function. Or maybe I misunderstand functions.

I need the info to write a simple app that gets a user's latitude / longitude, checks google's API to get the user's city name based on the lat / long, then checks the Dark Sky API to get your local weather based on the lat / long. So after this I have to figure out how to get multiple callbacks to play together.

Link to Gist of Mr. Beuchler's (helpful but I still don't get it) response to me: https://gist.github.com/theednaffattack/25d24d620cc35b160c17a95756fd1927

Community
  • 1
  • 1

1 Answers1

0

If I understand the question correctly, you intend to get the name of the function being passed as a callback. If your function looks like this:

function myFunc(/*some parameters*/) {
    //Do something
}

then you can get its name using myFunc.name in most web-browsers. To make this bullet-proof, you will need a function like this:

function getFunctionName(func) {
    if (typeof func === "function") { //Make sure the input is a function
        if (func.name) { //This is a named function
            return func.name;
        }
        var text = func + "";
        text = text.substring(0, text.indexOf("(")).trim();
        var spaceIndex = text.indexOf(" ");
        if (spaceIndex > 0) { //We need something like function myFunc
            return text.substring(text.indexOf(" ")).trim();
        }
        return ""; //Does not have a name
    }
    return false; //Not a function
}

Now

console.log(getFunctionName(myFunc));

will yield

myFunc

console.log(getFunctionName());

will yield

false

console.log(function() {});

will yield empty string, as the function does not have a name.

If we define something like this

function something() {
    this.somethingElse = function() {};
}

then

console.log(getFunctionName((new something()).somethingElse));

will yield empty string. Clearly, we would prefer it to yield somethingElse, but we need additional tweaks for this. Suggestion:

function nameFunction(func, name) {
    func.funcionName = name;
    return func;
}

and then

function something() {
    this.somethingElse = nameFunction(function() {}, "somethingElse");
}

and then change the function we have been using to

function getFunctionName(func) {
    if (typeof func === "function") { //Make sure the input is a function
        if (func.name) { //This is a named function
            return func.name;
        } else if (typeof func.functionName === "string") { //We called nameFunction on this
            return func.functionName;
        }
        var text = func + "";
        text = text.substring(0, text.indexOf("(")).trim();
        var spaceIndex = text.indexOf(" ");
        if (spaceIndex > 0) { //We need something like function myFunc
            return text.substring(text.indexOf(" ")).trim();
        }
        return ""; //Does not have a name
    }
    return false; //Not a function
}

now you are ready to call getFunctionName from getData like this:

function getData(dataURI, thingToRun) {
  var myData = getSomeData();
  console.log(getFunctionName(thingToRun));
  thingToRun(myData);
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175