1

I am trying to pull out a response from a query in a promise, but I cannot for the life of me, get the promise to resolve. It is stuck in pending.

db is my connection to the local database I am using. It is working.

Anyone know what I am doing wrong? What do I need for my promise to resolve?

const retrieveDepartments = new Promise(function (resolve, reject) {
    db.query(`SELECT department_name FROM departments;`, function (err, response) {
        if (response === undefined) {
            reject(new Error('Data is undefined'));
        } else {
            const departmentList = response.map(department => department.department_name)
            console.log(departmentList)
            resolve(departmentList);
        }
    })
})

retrieveDepartments
    .then((values) => {
        return values
    })
    .catch(Error)

console.log(retrieveDepartments)
  • 1
    Put the console.log in the `.then()`. – zero298 Feb 03 '22 at 20:30
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – zero298 Feb 03 '22 at 20:30
  • not sure what `Error` is in your catch, but maybe try logging it since its possibly throwing an error. `.catch(err => { console.log(err) });` – Andrew Lohr Feb 03 '22 at 20:30
  • Promises are promises all the way down. Consoling a promise that you just made is always going to give you a pending promise, and never an actual value. You have to register a callback that will be called when the promise fulfills. – zero298 Feb 03 '22 at 20:31
  • Then we need more details, what *does* it do? You say it's stuck pending, does it ever get into the mapping of the response? Are you sure your SQL server is working? Can you trace whether it is doing anything? – zero298 Feb 03 '22 at 20:35

0 Answers0