✅ – ✅ – 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
36 Replies
beautifulpython
beautifulpythonOP2y 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
}
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
yes sir
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
this is for trino database and when using rest api youknow it has ended or drained when d.nextUri is not longer there
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
do I need some type of event listener for this onEnd ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
anonymous function no name
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
i cant bcs its anonymous i need to naem it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
oh sorry. i was going to dump it here
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
when it is done
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
function onEnd(){
return orders
}
function onEnd(){
return orders
}
onEnd is not a function let me read up on this some more. thanks!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ghardin137
ghardin1372y ago
Or you could just await the drainTrino call inside the function And merge what it returns with orders And return that
beautifulpython
beautifulpythonOP2y ago
ok let me try both. brb lunch time yes some init stuff does call it indeed. looking great @slightlytyler ! thanks
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y ago
ok, part of learning so this gets me closer to the polished version of @ghardin137 @ghardin137 liek this? await drainTrino(d.nextUri)
ghardin137
ghardin1372y ago
Yeah but you need to get accept the return value Then merge that with the other orders And return that
beautifulpython
beautifulpythonOP2y ago
ok, let me give that a spin I think I am stuck so I will hand this over to a more experiences dev
ghardin137
ghardin1372y 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
beautifulpython
beautifulpythonOP2y 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
ghardin137
ghardin1372y 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 🙂
beautifulpython
beautifulpythonOP2y ago
duh! you are right
ghardin137
ghardin1372y ago
yup recursion can sometimes be pretty awesome
beautifulpython
beautifulpythonOP2y ago
"sometimes" is the key word. inception movie comes to mind 😆
ghardin137
ghardin1372y ago
oh yes it can also be a complete nightmare
beautifulpython
beautifulpythonOP2y 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
reactibot
reactibot2y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1133483358408687657
reactibot
reactibot2y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1133483358408687657 This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1133483358408687657