5

It is to my understanding that callback functions are asynchronous and cannot return a value like regular functions. Upon reading about promises, I thought I grasped a good understanding about them, that they are basically an enhanced version of callbacks that allows returning a value like asynchronous function. In my getConnections method, I am attempting to call the find() function on my database through mongoose, and I am attempting to grab this array of objects and send it to the views.

var test =  new Promise((resolve, reject) => {
        Database.find().then(list => {
            resolve(list);

        }).catch(err=> {
            return reject(err);
        })

        })
console.log(test)

When I attempt to log to the console outside of the promise function, I get Promise { _U: 0, _V: 0, _W: null, _X: null }

I don't think this is functioning correctly, and I thought I utilized promises correctly. Could anyone point me in the right direction on how to return this array of objects outside of the callback function?

Syntle
  • 5,168
  • 3
  • 13
  • 34
Calamity
  • 53
  • 1
  • 1
  • 6
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Anurag Srivastava Apr 17 '20 at 22:31
  • 1
    Try this instead: `test.then(x=> console.log(x));`. [then](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) is the normal mechanism to get the result of the promise once it resolves. – David784 Apr 17 '20 at 22:50
  • Well I get the console log but I want to access this variable outside the document and send it to views. – Calamity Apr 17 '20 at 23:13
  • rethink your code ... think like this ... *asynchrony can not be made synchronous, because I'm writing javascript, not developing a time machine* :D – Jaromanda X Apr 18 '20 at 00:12
  • *send it to views* sounds something you could wrap into `then()`. – tevemadar Apr 18 '20 at 08:15
  • `Database.find().then(list => list)` <-- list is what you need – Deryck Apr 18 '20 at 13:15

4 Answers4

3

You can simply add await before the promise declaration. i.e.

var test = await new Promise...
Karim A. Wazzan
  • 209
  • 1
  • 5
2

The thing is that when you write a function like this:

const getData = async () => { const response = await fetch(someUrl, {}, {}); return response;}

Then you also need to await that function when you call it. Like this:

const setData = async () => { const dataToSet = await getData(); }
1
let test =  new Promise((resolve, reject) => {
        Database.find().then(list => {
            resolve(list);

        }).catch(err=> {
            return reject(err);
        })

        })

test
.then(result=>console.log(result))

Should solve your problem.

Algo7
  • 2,122
  • 1
  • 8
  • 19
1
    var someValue;
    var test =  await new Promise((resolve, reject) => {
        Database.find().then(list => {
           resolve(list);

         }).catch(err=> {
           return reject(err);
       })

    }).then(res => {
      someValue=res;
    })

   console.log(someValue);
Surojit Paul
  • 1,232
  • 9
  • 11