0

I'm trying to get a returned variable from inside a function and us it in the rest of the page, i'm getting no luck at all, is there a way to get "AmazonvastTagUrl" from inside the function, outside the function, so as I can use it later down the page?

this what have so far:

var AmazonvastTagURL = 'https://pubads.g.doubleclick.net/gampad/ads?cust_params='
console.log('%cAdysis%c Amazon AdTag:', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;", "", AmazonvastTagURL);

// Request Bids 

apstag.fetchBids({
    slots: [{
      slotID: 'SOMESLOT',
      mediaType: 'video'
      //Slot name created in the portal, aligns to individual request for this bid 
    }],
    timeout: PREBID_TIMEOUT / 1.5
  },
  function(bids) {
    console.log('%cAdysis%c Amazon Pre-Roll Bid Logs', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;", "", bids);
    // console.log('%cAdysis%c Amazon Pre-Roll Bid Logs', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;","", encodedQsParams);
    //Pass bids into the function that will append the key values onto the VAST tag 
    setTimeout(() => {
      handleVideoBids(bids);
    }, 100)
  });


function handleVideoBids(bids) {
  if (bids.length > 0) {
    AmazonvastTagURL += bids[0].encodedQsParams;

    console.log('%cAdysis%c Amazon encodedQsParams Params:', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;", "", AmazonvastTagURL);
  }
};

this get the log out of enter image description here

  • got an example, as that what i put above works for "inside the function" it consolue logs the results – David Stanton Apr 11 '23 at 16:07
  • You've got a function with no name (which should be a syntax error), then two functions with the same name. It's not clear what's going on, but I have the suspicion that you're trying to call some asynchronous service to set up the value of a global variable, which is a very common mistake (because you really can't do that). – Pointy Apr 11 '23 at 16:08
  • just edited it, as had a duplicate in – David Stanton Apr 11 '23 at 16:09
  • Why use `setTimeout()`? – Barmar Apr 11 '23 at 16:17
  • why dont you just return it? return AmazonvastTagURL – Vishal Apr 11 '23 at 16:18
  • @Vishal That won't work, it's an async function. – Barmar Apr 11 '23 at 16:20
  • @Barmar declare a variable above fetchbids function, return it so you have something like 'myVariable = handleVideoBids(bids)' – Vishal Apr 11 '23 at 16:23
  • @Vishal That won;'t work, `bids` is local to the callback function in `fetchbids()`. – Barmar Apr 11 '23 at 16:26
  • @Vishal I dont know how to "just return", I aint very good at this stuff, this?: function handleVideoBids(bids) { if (bids.length > 0) { AmazonvastTagURL += bids[0].encodedQsParams; return AmazonvastTagUrl console.log('%cAdysis%c Amazon encodedQsParams Params:', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;","", AmazonvastTagURL); } }; – David Stanton Apr 11 '23 at 16:28
  • @Barmar if you have a variable and you assign it the value at this point 'myVariable = handleVideoBids(bids)' inside timeout function, why wont it work ? The only need is that myVariable should be declared at the top. Where am i going wrong? – Vishal Apr 11 '23 at 16:43
  • See the linked questions. The assigned variable won't be available after the call because it runs asynchronously. @Vishal – Barmar Apr 11 '23 at 16:50
  • @Barmar what the solution then, baring in mind i aint no good at this stuff, so an example would be highly appreciated – David Stanton Apr 11 '23 at 16:51
  • @Barmar so declare it near var AmazonvastTagURL as 'var myVariable=null'. Then it will hold the value wont it? – Vishal Apr 11 '23 at 16:53
  • @Vishal You're not understanding the general problem of async code. The callback doesn't run until all the synchronous code finishes. So it sets the variable *after* you try to read it. – Barmar Apr 11 '23 at 16:57
  • @DavidStanton See the linked questions. – Barmar Apr 11 '23 at 16:57
  • @Barmar but you handleVideoBids in the timeout. Why cant you retrieve the value in it by returning it from the function and setting it to a file level variable? – Vishal Apr 11 '23 at 17:07
  • @Vishal Because the callback runs asynchronously. It runs *after* the function has already returned, so its return value isn't used. – Barmar Apr 11 '23 at 17:11
  • @Barmar all those other questions/ answers are complete "foriegn language" to me, i not got a clue, I aint any good at this stuff, a simple example is about the only thing i can follow, my brain aint got that capacity unfortunatly – David Stanton Apr 11 '23 at 17:19
  • @DavidStanton as per the logs 'AmazonvastTagURL' is updated. What is the issue? – Vishal Apr 11 '23 at 17:21
  • @DavidStanton It's simple. Anything that needs to use the response immediately must be in the callback. You can set the global variable as well, but its updated valiue won't be available immediately. You can use it in other event listeners, which will run later when the user performs some interactions. – Barmar Apr 11 '23 at 17:23
  • i need to take AmazonvastTagURL returned values and use later down the page, outside the function – David Stanton Apr 11 '23 at 17:24
  • @Barmar its simple if you know what your doing, as In brain surgery is simple if you know what your doing, as said 3 times, I dont, examples are things I "can follow" – David Stanton Apr 11 '23 at 17:27
  • @DavidStanton at the top declare a variable 'var returnedBids= null' then set 'returnedBids = bids' below the callback 'function(bids) {' and above 'console.log('%cAdysis%c....' . That should give you access to bids data – Vishal Apr 11 '23 at 17:29
  • If you do `button.onclick=function() {console.log(AmazonvastTagURL); }` you'll see that clicking the button displays the value of the variable as desired. – Barmar Apr 11 '23 at 17:30
  • @Vishal dont seem to work:function(bids) { returnedBids = bids console.log('%cAdysis%c Amazon Pre-Roll Bid Logs', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;","", bids); // console.log('%cAdysis%c Amazon Pre-Roll Bid Logs', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;","", encodedQsParams); //Pass bids into the function that will append the key values onto the VAST tag setTimeout(() => { handleVideoBids(bids); }, 100) }); – David Stanton Apr 11 '23 at 17:50
  • @DavidStanton I think it should work because otherwise data would be lost for updated AmazonVastTagURL from this line 'AmazonvastTagURL += bids[0].encodedQsParams;' as well – Vishal Apr 11 '23 at 19:00
  • it dont work, its wont consolue log out furtehr down the page – David Stanton Apr 11 '23 at 19:03
  • i think the problem is that amazon has a variable of bid, which is actually getting consolue logged with:console.log('%cAdysis%c Amazon Pre-Roll Bid Logs', "background: rgb(19 177 118);color:#FFF;padding:5px;border-radius: 5px;line-height: 10px;","", bids); this shows an "array" or params not just the one need which is encodedQsParams – David Stanton Apr 11 '23 at 19:40

0 Answers0