简体   繁体   中英

Promise is always undefined when resolving for async function

I'm doing a database query in an async function, then calling it from another file with a promise . But the result of the promise is always undefined .

My db function:

async function findSomething(id) {
   var query = "my_query";
   connection.query(query);
};

This is how I'm calling it from another file:

DbUtil.findSomething(1).then(function(result, error) {
   if(!error) {
    console.log("the result " + result);
  }
});

The result is always undefined .

Note that the calling function, DbUtil.findSomething is called from another file. I do not want to chain it inside the findSomething() function.

What am I missing?

You are missing a return statement.

Any function without one returns undefined .

Since findSomething is as async function, the return value is used to resolve the promise it returns.

async function findSomething(id) {
  var query = "my_query";
  return connection.query(query);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM