-3

I'm trying to make a callback function, and i can't find a solution to pass an argument in this callback ..

var chrome = {
  enterprise: {
    deviceAttributes: {
      getDirectoryDeviceId: function() {
        return "test";
      }
    }
  }
};

chrome.enterprise.deviceAttributes.getDirectoryDeviceId(function(deviceid) {
  alert(deviceid);
});

How can the deviceid in the callback function be "xxx" ?

Many thanks

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Adrien QUINT
  • 589
  • 4
  • 18
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Sébastien Jan 05 '18 at 21:58
  • 2
    `getDirectoryDeviceId` doesn't accept an argument, so the argument you're passing in is ignored. –  Jan 05 '18 at 21:58
  • 2
    This is not a duplicate of that question. There is no async code here. –  Jan 05 '18 at 21:59
  • `How can the deviceid in the callback function be "xxx" ?` i mean... the more important question is why is the callback function being called for you to know the deviceid is xxx? – Kevin B Jan 05 '18 at 22:02
  • 1
    The snippet you posted wont call the callback. Try posting a relevant code so that we can say why its alerting xxx – karthick Jan 05 '18 at 22:05
  • Duplicate of https://stackoverflow.com/questions/824234/what-is-a-callback-function – Sébastien Jan 05 '18 at 22:06
  • @karthick that isn't how i interpreted the question. I don't think he's saying it's alerting `xxx`. I think he's asking how to set the `deviceid` in the callback to a specific value. "How can I make it be 'xxx'?" –  Jan 05 '18 at 22:12
  • May be useful: http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ – jarmod Jan 05 '18 at 22:20

2 Answers2

2

You are passing a function into getDirectoryDeviceId when you call it...but not calling that function or accepting it as an argument

Try:

var chrome = {
  enterprise: {
    deviceAttributes: {
      getDirectoryDeviceId: function(callback) {
                                   // ^^ function you already pass in when you call method

        // call the callback you pass in below
        callback("test");
      }
    }
  }
};
// nothing changed here
chrome.enterprise.deviceAttributes.getDirectoryDeviceId(function(deviceid) {
  alert(deviceid);
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

First, chrome.enterprise.deviceAttributes.getDirectoryDeviceId is being assigned a function that takes no arguments, so you'd need to set one up there.

Then, when you call getDirectoryDeviceId the function that you pass can be executed by appending () to the argument.

Lastly, if you want to be able to pass an argument that the callback will use, you need to set up a second argument in the function.

var chrome = {
   enterprise: {
    deviceAttributes: {
      getDirectoryDeviceId : function(callback, data){
        callback(data);
      }
    }
  }
};


chrome.enterprise.deviceAttributes.getDirectoryDeviceId(function (deviceid){
  alert(deviceid);
}, "TEST!");  // <-- Second argument is the data that will be used by the callback
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71