Click here to Skip to main content
14,975,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
function Sti(addr, tag, id, tt) {
  let the;
  (async () => {
      let response = await fetch('http://funapi.eu5.org/our_api.php?addr='+addr+'&id='+id+'&tag='+tag+'&tt='+tt);

      let text = await response.text();

      the = text;
  })()
  return the;
}

let then = Sti("https://stackoverflow.com", "div", "left-sidebar", "html");

document.getElementById('h3').innerHTML = then;

Tryit Editor v3.6[^]
after running this code the
HTML
<p id=h3></p>
block just shows "undefined" why?? I normally use var instead of let recently started using let I noticed some subtle differences is this because of that??

What I have tried:

I tried using var to declare the variable inside the async function!!
https://i.imgur.com/Oa4xKVe.png
this is what console shows.
Posted
Updated 14-Jun-21 4:31am
v2

Your Sti function returns before the inner async function has completed.

You can't write a sync-over-async wrapper in Javascript. You need to make the Sti function async, and change the calling code to await the result.
JavaScript
(async function(){
    async function Sti(addr, tag, id, tt) {
        let response = await fetch(...);
        let text = await response.text();
        return text;
    }
    
    let then = await Sti("https://stackoverflow.com", "div", "left-sidebar", "html");
    document.getElementById('h3').innerHTML = then;
})();
You'll have two other issues running that code on TryItEditor:
  • You're trying to load an http: URL from an https: site. Your browser will probably block that request.
  • The URL you're trying to load doesn't allow cross-origin requests.
   
Comments
Ɗααɳιടԋ Sყҽԃ 14-Jun-21 10:38am
   
Thanks it works but is there a way we could use return the variable
`then` outside the `async` `function()` ?
Richard Deeming 14-Jun-21 10:40am
   
Is there a way you can use a variable before it has been set? No.
If you build the URL based on the above, you get http://funapi.eu5.org/our_api.php?addr=https://stackoverflow.com&id=left-sidebar&tag=div&tt=html[^]. Paste that into your browser to check the result.
   
Comments
Ɗααɳιടԋ Sყҽԃ 13-Jun-21 7:09am
   
that is what i want
Richard MacCutchan 13-Jun-21 7:13am
   
What is?
Ɗααɳιടԋ Sყҽԃ 13-Jun-21 7:15am
   
i wanted to get this https://imgur.com/ke5agRB
Richard MacCutchan 13-Jun-21 7:16am
   
Well you got it, so now what do you want to do?
Ɗααɳιടԋ Sყҽԃ 13-Jun-21 11:30am
   
I want to get it in the JavaScript code, My question clearly says that the function returns "un defined" i want it to return what the page "http://funapi.eu5.org/our_api.php?addr=https://stackoverflow.com&id=left-sidebar&tag=div&tt=html" consists of!!
Richard MacCutchan 13-Jun-21 11:45am
   
I have tried a few variations on your script but it fails every time. I have a suspicion it is something to do with the async calls and the fetch method. Sadly I am not a Javascript expert so am out of other ideas. You need to do some more debugging to find out why it is not returning any data.
Its seems your code is clear but the issue causing when you assigning the response from the url to variable.
let text = await response.text();

Solution:
1. check the generated URL by your code whether working or not and retuning proper response

2. Pre-check the response before assigning to var or let

3. If URL response contains any property with your HTML or TEXT value then you have to assign the property to your variable not a full response.
   
v2
Comments
Richard MacCutchan 14-Jun-21 8:15am
   
Please do not use bold text for all the answer. People here are capable of reading normal weight fonts.
Ɗααɳιടԋ Sყҽԃ 14-Jun-21 10:32am
   
no that is working without the Sti() function
You should try using this:
JavaScript
var the;

instead of
JavaScript
let the;
   
Comments
Ɗααɳιടԋ Sყҽԃ 12-Jun-21 16:15pm
   
I've tried that it still doesn't work
A-Games 12-Jun-21 18:16pm
   
Oh. Well, i experemented with ur code some more and i dont know :\
A-Games 12-Jun-21 18:16pm
   
srry
Ɗααɳιടԋ Sყҽԃ 13-Jun-21 7:05am
   
no prob
A-Games 25-Jun-21 22:34pm
   
im not good with PHP

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900