4

I keep getting Promise { < pending > } on the console.log, I am not too experienced at all with Async functions. (Using JavaScript, Node, Mongodb)

function resolveAfter1() {
  return new Promise(resolve => {
    var scoresFromDb = db.account.find({}, { username: 1, score: 1 }).toArray(function(err, result) {
          if (err) throw err;
          // return result;
    })
    setTimeout(() => {
      resolve('resolved');
    }, 1000);
  });
}

async function asyncCall() {
  var result = await resolveAfter1();
}

asyncCall();

console.log(asyncCall());
Andrew
  • 603
  • 1
  • 5
  • 19
  • you will have `result` as the resolved value inside your `asyncCall()` function – Faizuddin Mohammed Feb 09 '18 at 11:33
  • 1
    Your log for `asyncCall()` will be just undefined as the function is not returning anything. – Faizuddin Mohammed Feb 09 '18 at 11:34
  • 1
    All functions marked with the `async` keyword return promises. That's what the `async` keyword do - it is a promise constructor with the added feature that you can use the `await` keyword inside it. So to `console.log` you need to call it like a regular promisified function: `asyncCall().then(x => console.log(x))` – slebetman Feb 09 '18 at 11:36
  • Like this? `async function asyncCall() { var result = await resolveAfter1(); return result; }` – Andrew Feb 09 '18 at 11:37
  • Thank you all for feedback! – Andrew Feb 09 '18 at 11:40

2 Answers2

5

If you call async function from non async context, it's same as using a promise.

IE. this:

async function something() {return 0;}

var result = something();

is almost the same as this:

function something() {
    return new Promise((resolve, reject)=>{resolve(0);});
}

var result = something();

In both cases, result will be a Promise, you can try it in console.

This means, that if you want to get the result, you need to do it from async function:

async function asyncCall() {
  var result = await resolveAfter1();
  console.log(result);
}

To access async result from since context, you still need to go with good 'ol .then and .catch:

resolveAfter1()
    .then((result)=>{console.log(result);})
    .catch((error)=>{console.log(error);})

Additionally, you should NOT throw in promise, that's what reject is for:

function resolveAfter1() {
  return new Promise((resolve, reject) => {
    var scoresFromDb = db.account.find({}, { username: 1, score: 1 }).toArray(function(err, result) {
          if (err) 
              reject(err);
          else
              resolve(result);
    });
  });
}
Giulio Bambini
  • 4,290
  • 4
  • 18
  • 34
0
function resolveAfter1() {
  return new Promise(resolve => {
    var scoresFromDb = db.account.find({}, { username: 1, score: 1 }).toArray(function(err, result) {
          if (err) throw err;
          // return result;
    })
    setTimeout(() => {
      resolve('resolved');
    }, 1000);
  });
}

async function asyncCall() {
  var result = await resolveAfter1();
  console.log(result); // here's your result
  return result;
}

asyncCall().then(result => console.log(result));

// console.log(asyncCall());
Faizuddin Mohammed
  • 3,650
  • 5
  • 21
  • 37