R
Reactiflux

✅ – ✅ – beautifulpython – 19-38 Jul 25

✅ – ✅ – beautifulpython – 19-38 Jul 25

Bbeautifulpython7/25/2023
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
}
}
UUUnknown User7/25/2023
Message Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
yes sir
UUUnknown User7/25/2023
3 Messages Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
this is for trino database and when using rest api youknow it has ended or drained when d.nextUri is not longer there
UUUnknown User7/25/2023
Message Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
do I need some type of event listener for this onEnd ?
UUUnknown User7/25/2023
Message Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
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
UUUnknown User7/25/2023
Message Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
anonymous function no name
UUUnknown User7/25/2023
Message Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
i cant bcs its anonymous i need to naem it
UUUnknown User7/25/2023
2 Messages Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
oh sorry. i was going to dump it here
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
drainTrino(d.nextUri)
} else {
() => {
return orders
};
}
UUUnknown User7/25/2023
Message Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
when it is done
UUUnknown User7/25/2023
3 Messages Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
function onEnd(){
return orders
}
function onEnd(){
return orders
}
onEnd is not a function let me read up on this some more. thanks!
UUUnknown User7/25/2023
4 Messages Not Public
Sign In & Join Server To View
Gghardin1377/25/2023
Or you could just await the drainTrino call inside the function And merge what it returns with orders And return that
Bbeautifulpython7/25/2023
ok let me try both. brb lunch time yes some init stuff does call it indeed. looking great @slightlytyler ! thanks
UUUnknown User7/25/2023
2 Messages Not Public
Sign In & Join Server To View
Bbeautifulpython7/25/2023
ok, part of learning so this gets me closer to the polished version of @ghardin137 @ghardin137 liek this? await drainTrino(d.nextUri)
Gghardin1377/25/2023
Yeah but you need to get accept the return value Then merge that with the other orders And return that
Bbeautifulpython7/26/2023
ok, let me give that a spin I think I am stuck so I will hand this over to a more experiences dev
Gghardin1377/26/2023
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
Bbeautifulpython7/26/2023
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
Gghardin1377/26/2023
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 🙂
Bbeautifulpython7/26/2023
duh! you are right
Gghardin1377/26/2023
yup recursion can sometimes be pretty awesome
Bbeautifulpython7/26/2023
"sometimes" is the key word. inception movie comes to mind 😆
Gghardin1377/26/2023
oh yes it can also be a complete nightmare
Bbeautifulpython7/27/2023
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
UUUnknown User7/29/2023
4 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – ✅ – beautifulpython – 19-38 Jul 25

Join Server