0

I'm currently trying to build a discord bot, and I want to use a database for some aspects of it. Currently, I'm trying to add a command that would return the names of all the tables I have in the database, and for the most part I have it down.

The part that I'm struggling with is actually getting the names back out as a var. Every guide and stackoverflow question that I've been able to find on it assume that you just want to get that result and then print it to the console, but I need to return it back to the method that called this.

My previous attempt was setting an outside variable and using a promise to wait for it to change, but I couldn't get that to work, most likely because I don't fully understand how Promises work. My current attempt uses setTimeout() to check, but that just returns the asyncID of either the first or second iteration.

Any help either in making either of these work or completely scrapping them and doing this a different way is very welcome.

Previous code:

function listTables() {
    db.query('SELECT table_name FROM information_schema.tables WHERE table_schema=\'' + dbName + '\'', (error, results) => {
        if(error) throw error;
        let temp = '';
        results.forEach((item) => {
            temp += item.table_name + ', ';
        }); temp = temp.slice(0, -2);
        setReturn(temp);
    });
    
    let out = checkReturn().then((value) => {
        return value();
    }).catch((error) => {
        console.log(error);
        return '';
    });
    returnValue = null;
    return out;
}



var returnValue = null;
function setReturn(value) {
    returnValue = value;
}
async function checkReturn() {
    console.log('Checking Return: ' + returnValue);
    let promise = new Promise((resolve, reject) => {
        if(returnValue === null) reject('Var not set');
        else resolve(returnValue)
    });
    return await promise;
}

Current Code:

function listTables() {
    setReturn(null);
    db.query('SELECT table_name FROM information_schema.tables WHERE table_schema=\'' + dbName + '\'', (error, results) => {
        if(error) throw error;
        let temp = '';
        results.forEach((item) => {
            temp += item.table_name + ', ';
        }); temp = temp.slice(0, -2);
        setReturn(temp);
    });
    return checkReturn();
}



var returnValue = null;
function setReturn(value) {
    returnValue = value;
}
function checkReturn() {
    console.log('Checking Return: ' + returnValue);
    if(returnValue === null) {
        return setTimeout(checkReturn, 50);
    } else {
        return returnValue;
    }
}

1 Answer 1

1

You need to modify the listTables function to return a promise.

function listTables() {
    return new Promise((resolve, reject) => {
        db.query('SELECT table_name FROM information_schema.tables WHERE table_schema=\'' + dbName + '\'', (error, results) => {
            if(error) {
                reject(error);
                return;
            }
            let temp = '';
            results.forEach((item) => {
                temp += item.table_name + ', ';
            }); temp = temp.slice(0, -2);
            resolve(temp);
        });
    });
}

// Usage of `listTables()`
listTables()
    .then(result -> {
        // Do process result
    });
Sign up to request clarification or add additional context in comments.

1 Comment

And that was exactly what I needed for promises to click. Thank you much, it works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.