✅ – ✅ – 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
B
beautifulpython268d ago
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
}
}
UU
Unknown User268d ago
B
beautifulpython268d ago
yes sir
UU
Unknown User268d ago
B
beautifulpython268d ago
this is for trino database and when using rest api youknow it has ended or drained when d.nextUri is not longer there
UU
Unknown User268d ago
B
beautifulpython268d ago
do I need some type of event listener for this onEnd ?
UU
Unknown User268d ago
B
beautifulpython268d ago
ok let me read up on that how to implement sucj a thing. thnx! can this work?
() => {
return orders
};
() => {
return orders
};
oh wait it has to be passed on got it
UU
Unknown User268d ago
B
beautifulpython268d ago
anonymous function no name
UU
Unknown User268d ago
B
beautifulpython268d ago
i cant bcs its anonymous i need to naem it
UU
Unknown User268d ago
B
beautifulpython268d ago
oh sorry. i was going to dump it here
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
UU
Unknown User268d ago
B
beautifulpython268d ago
when it is done
UU
Unknown User268d ago
B
beautifulpython268d ago
function onEnd(){
return orders
}
function onEnd(){
return orders
}
onEnd is not a function let me read up on this some more. thanks!
UU
Unknown User268d ago
G
ghardin137268d ago
Or you could just await the drainTrino call inside the function And merge what it returns with orders And return that
B
beautifulpython268d ago
ok let me try both. brb lunch time yes some init stuff does call it indeed. looking great @slightlytyler ! thanks
UU
Unknown User268d ago
B
beautifulpython268d ago
ok, part of learning so this gets me closer to the polished version of @ghardin137 @ghardin137 liek this? await drainTrino(d.nextUri)
G
ghardin137268d ago
Yeah but you need to get accept the return value Then merge that with the other orders And return that
B
beautifulpython267d ago
ok, let me give that a spin I think I am stuck so I will hand this over to a more experiences dev
G
ghardin137267d ago
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
}
}
something like that should work you don't need the external orders variable
B
beautifulpython267d ago
thanks you so much
(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
}
}
the reason I needed orders as global is I need to return it to my calling app. in this case grafana
G
ghardin137267d ago
you can still do that it's the returned value of the drainTrino function 🙂 technically you don't even need that first bit
(async () => {
const orders = await drainTrino('http://localhost:8080/v1/statement');
})();
(async () => {
const orders = await drainTrino('http://localhost:8080/v1/statement');
})();
that would work exactly the same 🙂
B
beautifulpython267d ago
duh! you are right
G
ghardin137267d ago
yup recursion can sometimes be pretty awesome
B
beautifulpython267d ago
"sometimes" is the key word. inception movie comes to mind 😆
G
ghardin137267d ago
oh yes it can also be a complete nightmare
B
beautifulpython267d ago
looks like there is an issue in the else part. no worries I will run with it you have guided me enough. thanks!
# 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:
thanks so much @ghardin137 learned a lot. final product is here
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
}
}
the first drain call requires a POST instead of a GET
UU
Unknown User265d ago