New Request from multiple Sources

I’m trying to create a widget from my Plausible Analytics data.

I would like to get realtime info as well as the filtering other data so it requires two different urls.

I’ve created two bits of code to get the data but I am duplicating code to do so, I’m sure there is a better way to do this by combing the code.

They’re using the same Method and Headers so can I combine these to make a single piece of code?

const reqRealtime = new Request(realtimeURL)
reqRealtime.method = "GET"
responseRealtime = await reqRealtime.loadString()
const resultRealtime = JSON.parse(responseRealtime);

const reqStats = new Request(statsURL)
reqStats.method = "GET"
responseStats = await reqStats.loadString()
const resultStats = JSON.parse(responseStats);

You could use a function so you are just maintaining one set of code to carry out the actions and pass the URL in on each call. But you can’t exactly combine (your combing typo did make me laugh) them intrinsically and get better combined performance if that’s what you were thinking.

1 Like

You can get a slight performance increase by awaiting the requests together as opposed to individually (assuming the second request doesn’t rely on data obtained in the first request). Essentially you want loadString for each request called as early as possible.

Using Promise.all is the cleanest way I could think of to accomplish this

async function sendRequest(url) {
  const req = new Request(url);
  req.method = "GET";
  req.headers = {
    "Authorization": api,
    "Content-Type": "application/json",
  const res = await req.loadString();
  return JSON.parse(res);

const [
] = await Promise.all([


1 Like

Perfect, that’s works a treat. thank you.