0

I was trying to return an array of data from python function to Javascript. I am using eel python module. The python function returns a string with ease, but when I tried to return an array object, it returns nothing to javascript.

Here is the python function:

@eel.expose

def get_list_data(column_name):
    tree = ET.parse('resources.xml')
    root = tree.getroot()

    column_list_data = []

    for child in root.findall('column'):
        if child.get('name') == column_name:
            for grandchild in child:
                column_list_data.append(grandchild.text)
    return(column_list_data)

And here is the javascript function.

function getListData(){
    let retData = eel.get_list_data("Response")();
    console.log(retData);
}

getListData();

Here is the console log shows.

Promise
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Array(0)
  • It seems like it's returning a promise. Try adding `async` before calling in JS or use `Promise.resolve(...)` or `new Promise()` syntax. There are many ways to resolve a promise in JS (https://stackoverflow.com/questions/26711243/promise-resolve-vs-new-promiseresolve) – VPaul Apr 08 '20 at 13:43
  • Only declaring the function as asynchroneously like `async function getListData()` won't do the job. You'll have to use await on the asynchroneous promise which then makes your function asynchroneous as well and therefore requires the async declaration: `let retData = await eel.get_list_data("Response")();`. – Leo Apr 08 '20 at 13:57

1 Answers1

1

That is, because retData seems to be a Promise. Please consider reading the following (MDN Promise) for a deeper understanding of promises in javascript.

The promise is executed asynchronously. The code below the "then-callback" is executed in case that the promise has been resolved successfully. To give you something to start with:

function getListData(){
    let retData = eel.get_list_data("Response")()
       .then(retData => {
           console.log(retData);
       })
       .catch(e => console.log(e));
}
Leo
  • 1,508
  • 13
  • 27