R
Reactiflux
✅ – ✅ – beautifulpython – 19-38 Jul 25
✅ – ✅ – beautifulpython – 19-38 Jul 25
greetings, I have a self referencing async function that populates a global array declared top of my code
let final = []
But when I try to see it's value it still shows as []. Here is my code been at it for almost 4 hours and stuck
async function drainTrino(nextUri) {
let response = await fetch(nextUri,
{
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
}
)
let d = await response.json();
if (d.nextUri) {
if (d.data && d.columns) {
let o = {};
//let c = d.columns[0].name
//console.log('"' + c + '"')
for (let i = 0; i < d.data.length; i++) {
o["orderkey"] = d.data[i][0].toString();
o[`clerk`] = d.data[i][6];
o[`totalprice`] = d.data[i][3];
o[`orderdate`] = d.data[i][4];
orders.push(o)
}
}
drainTrino(d.nextUri)
} else {
return orders
}
}
async function drainTrino(nextUri) {
let response = await fetch(nextUri,
{
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
}
)
let d = await response.json();
if (d.nextUri) {
if (d.data && d.columns) {
let o = {};
//let c = d.columns[0].name
//console.log('"' + c + '"')
for (let i = 0; i < d.data.length; i++) {
o["orderkey"] = d.data[i][0].toString();
o[`clerk`] = d.data[i][6];
o[`totalprice`] = d.data[i][3];
o[`orderdate`] = d.data[i][4];
orders.push(o)
}
}
drainTrino(d.nextUri)
} else {
return orders
}
}
Message Not Public
Sign In & Join Server To View
yes sir
3 Messages Not Public
Sign In & Join Server To View
this is for trino database and when using rest api youknow it has ended or drained when
d.nextUri
is not longer thereMessage Not Public
Sign In & Join Server To View
do I need some type of event listener for this
onEnd
?Message Not Public
Sign In & Join Server To View
ok let me read up on that how to implement sucj a thing. thnx!
can this work?
oh wait it has to be passed on got it
() => {
return orders
};
() => {
return orders
};
Message Not Public
Sign In & Join Server To View
anonymous function no name
Message Not Public
Sign In & Join Server To View
i cant bcs its anonymous
i need to naem it
2 Messages Not Public
Sign In & Join Server To View
oh sorry. i was going to dump it here
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
Message Not Public
Sign In & Join Server To View
when it is
done
3 Messages Not Public
Sign In & Join Server To View
function onEnd(){
return orders
}
function onEnd(){
return orders
}
onEnd is not a function
let me read up on this some more. thanks!4 Messages Not Public
Sign In & Join Server To View
Or you could just await the drainTrino call inside the function
And merge what it returns with orders
And return that
ok let me try both. brb lunch time
yes some init stuff does call it indeed.
looking great @slightlytyler ! thanks
2 Messages Not Public
Sign In & Join Server To View
ok, part of learning so this gets me closer to the polished version of @ghardin137
@ghardin137 liek this?
await drainTrino(d.nextUri)
Yeah but you need to get accept the return value
Then merge that with the other orders
And return that
ok, let me give that a spin
I think I am stuck so I will hand this over to a more experiences dev
async function drainTrino(nextUri) {
let response = await fetch(nextUri,
{
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
}
)
let d = await response.json();
let orders = [];
if (d.data && d.columns) {
//let c = d.columns[0].name
//console.log('"' + c + '"')
orders = d.data.map(item => {
let o = {};
o["orderkey"] = item[0].toString();
o[`clerk`] = item[6];
o[`totalprice`] = item[3];
o[`orderdate`] = item[4];
return o;
})
}
if (d.nextUri) {
const nextOrders = await drainTrino(d.nextUri)
return [...orders, ...nextOrders];
} else {
return orders
}
}
async function drainTrino(nextUri) {
let response = await fetch(nextUri,
{
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
}
)
let d = await response.json();
let orders = [];
if (d.data && d.columns) {
//let c = d.columns[0].name
//console.log('"' + c + '"')
orders = d.data.map(item => {
let o = {};
o["orderkey"] = item[0].toString();
o[`clerk`] = item[6];
o[`totalprice`] = item[3];
o[`orderdate`] = item[4];
return o;
})
}
if (d.nextUri) {
const nextOrders = await drainTrino(d.nextUri)
return [...orders, ...nextOrders];
} else {
return orders
}
}
thanks you so much
the reason I needed orders as global is I need to return it to my calling app. in this case grafana
(async () => {
let response = await fetch('http://localhost:8080/v1/statement', {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
body: "select *, now() from tpch.sf1.orders order by orderkey asc limit 3",
headers: {
//'X-Trino-User': 'root',
'X-Trino-Catalog': 'tpch',
'X-Trino-Schema': 'sf1',
'accept': 'application/json',
'Authorization': 'Basic root'
}
})
let d = await response.json();
if (d.nextUri) drainTrino(d.nextUri);
})();
async function drainTrino(nextUri) {
let response = await fetch(nextUri,
{
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
}
)
let d = await response.json();
let orders = [];
if (d.data && d.columns) {
orders = d.data.map(item => {
let o = {};
o["orderkey"] = item[0].toString();
o[`clerk`] = item[6];
o[`totalprice`] = item[3];
o[`orderdate`] = item[4];
return o;
})
}
if (d.nextUri) {
const nextOrders = await drainTrino(d.nextUri)
return [...orders, ...nextOrders];
} else {
return orders
}
}
(async () => {
let response = await fetch('http://localhost:8080/v1/statement', {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
body: "select *, now() from tpch.sf1.orders order by orderkey asc limit 3",
headers: {
//'X-Trino-User': 'root',
'X-Trino-Catalog': 'tpch',
'X-Trino-Schema': 'sf1',
'accept': 'application/json',
'Authorization': 'Basic root'
}
})
let d = await response.json();
if (d.nextUri) drainTrino(d.nextUri);
})();
async function drainTrino(nextUri) {
let response = await fetch(nextUri,
{
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
}
)
let d = await response.json();
let orders = [];
if (d.data && d.columns) {
orders = d.data.map(item => {
let o = {};
o["orderkey"] = item[0].toString();
o[`clerk`] = item[6];
o[`totalprice`] = item[3];
o[`orderdate`] = item[4];
return o;
})
}
if (d.nextUri) {
const nextOrders = await drainTrino(d.nextUri)
return [...orders, ...nextOrders];
} else {
return orders
}
}
you can still do that
it's the returned value of the drainTrino function 🙂
technically you don't even need that first bit
that would work exactly the same 🙂
(async () => {
const orders = await drainTrino('http://localhost:8080/v1/statement');
})();
(async () => {
const orders = await drainTrino('http://localhost:8080/v1/statement');
})();
duh! you are right
yup recursion can sometimes be pretty awesome
"sometimes" is the key word. inception movie comes to mind
😆
oh yes
it can also be a complete nightmare
looks like there is an issue in the else part. no worries I will run with it you have guided me enough. thanks!
thanks so much @ghardin137 learned a lot. final product is here
the first drain call requires a
# Use root/example as user/password credentials
version: "3.1"
services:
db:
image: trinodb/trino
restart: always
ports:
- 8080:8080
networks:
- trino-net
networks:
trino-net:
# Use root/example as user/password credentials
version: "3.1"
services:
db:
image: trinodb/trino
restart: always
ports:
- 8080:8080
networks:
- trino-net
networks:
trino-net:
let body = 'select *, now() from tpch.sf1.orders order by orderkey asc limit 3';
(async () => {
const orders = await drainTrino('http://localhost:8080/v1/statement', 'POST', body);
console.log('from root', orders)
})();
async function drainTrino(nextUri, verb, body) {
let option = {
method: verb,
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
body: body,
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
};
if (body) {
option.body = body
};
let response = await fetch(nextUri, option);
let d = await response.json();
let orders = [];
if (d.data && d.columns) {
orders = d.data.map(item => {
let o = {};
o["orderkey"] = item[0].toString();
o[`clerk`] = item[6];
o[`totalprice`] = item[3];
o[`orderdate`] = item[4];
return o;
})
}
if (d.nextUri) {
const nextOrders = await drainTrino(d.nextUri, 'GET')
return [...orders, ...nextOrders];
} else {
return orders
}
}
let body = 'select *, now() from tpch.sf1.orders order by orderkey asc limit 3';
(async () => {
const orders = await drainTrino('http://localhost:8080/v1/statement', 'POST', body);
console.log('from root', orders)
})();
async function drainTrino(nextUri, verb, body) {
let option = {
method: verb,
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
body: body,
headers: {
'accept': 'application/json',
'Authorization': 'Basic root'
}
};
if (body) {
option.body = body
};
let response = await fetch(nextUri, option);
let d = await response.json();
let orders = [];
if (d.data && d.columns) {
orders = d.data.map(item => {
let o = {};
o["orderkey"] = item[0].toString();
o[`clerk`] = item[6];
o[`totalprice`] = item[3];
o[`orderdate`] = item[4];
return o;
})
}
if (d.nextUri) {
const nextOrders = await drainTrino(d.nextUri, 'GET')
return [...orders, ...nextOrders];
} else {
return orders
}
}
POST
instead of a GET
4 Messages Not Public
Sign In & Join Server To View
Looking for more? Join the community!
R
Reactiflux
✅ – ✅ – beautifulpython – 19-38 Jul 25
R
Reactiflux
✅ – ✅ – beautifulpython – 19-38 Jul 25
Want results from more Discord servers?
Recommended Posts✅ – ✅ – blackhorseh – 17-53 Jul 21Hey, guys, How to convert number 9700 to 9.7k ?
I know can convert it manually by dividing with 1000dmikester1 – 19-23 Jul 19anyone have any experience with masking inputs? I just want a very simple mask that looks like thissnowberb – 08-25 Jul 18Having an initially hidden element, if I use `scrollIntoView()` on that element after hitting a butthoangvu12 – 06-57 Jul 17Hi, I'm developing a chrome extension for a web page, I want two way communication between webapp an