S3AN556
S3AN556

Reputation: 79

Async function returns pending promise node js

This is a very easy question but no google search results return the correct answer.

const express = require("express");
const app = express();
const cors = require("cors");
const pool = require("./db");
const poolec2 = require("./db");
require("./function")();

returnMappings = async function(connection){
  try {
    let mapping = await connection.query("SELECT ticker FROM mappings");
    let results = await mapping.rows;
    //console.log(results);
    return results;
  } catch (err) {
    console.error(err.message);
  }
};

const mappings = returnMappings(poolec2);
console.log(mappings);

What am I missing here that is not returning my data? When I unquote console.log(results); I can see the desired results in my terminal. I've tried various versions of using .then but have not had any success return results.then;, const mappings = returnMappings(poolec2).then;, console.log(mappings.then);. I've also tried returning my results outside of my try catch with no luck. I'm really stuck trying to understand how I go about returning a from an async function.

EDIT

The goal is to pass the results from the above async function to another function to check if the a user inputted value exists in that vector of mappings. So indexOf based on a user input, I then use if else to return true or false. With the final results being either true or false.

checkMappings = function(string,input){
  stringArray = string;
  value = stringArray.indexOf(input);
  if(value > -1){
      return false
  }else{
      return true
  }
};

SOLUTION

returnMappings = async function(connection,input){
  try {
    const mapping = await connection.query("SELECT ticker FROM mappings_production");
    const results = await mapping.rows;
    //console.log(results);
    return results;
  } catch (err) {
    console.error(err.message);
  }
};

checkMappings = function(string,input){
  let stringArray = JSON.stringify(string);
  let value = stringArrayC1.indexOf(input);
  function test(a) {
    let check;
    if(a > -1) {
      return true
    }else {
      return false
    }
  };
  console.log(test(value));
  return test(value);
};

const resMappingCheck = returnMappings(poolec2).then((mappings) => checkMappings(mappings,"AAPL"));

console.log(resMappingCheck);

this worked for what I needed to do

Upvotes: 1

Views: 167

Answers (2)

David Miner
David Miner

Reputation: 318

As others have pointed out, await can only be used in an async function, but using .then() is functionally equivalent.

This syntax that should work for you:

returnMappings(poolec2).then((mappings) => console.log(mappings));

if you want to do something more elaborate / multi-line, you can use curly braces like so:

returnMappings(poolec2).then((mappings) => {
  console.log(mappings)
});

UPDATE:

If you want to chain two functions together, then you'll need to start with the .then() pattern: you can declare the callback function in .then() to be asynchronous. At that point, you can start to use await like you're used to.

I'm not sure what relationship you're trying to create between returnMappings() and checkMappings(), but you can chain them together like this: (note the use of async on the first line to allow the use of await inside the callback.)

returnMappings('test').then(async (mapping) => {
  const checkResult = await checkMappings(mapping)
  console.log(`checkMapping result: ${checkResult}`)
}).catch((err) => console.log(`Error: ${err}`))

Upvotes: 2

see sharper
see sharper

Reputation: 12055

Try this:

const mappings = await returnMappings(poolec2);

That will work if you wrap the code inside an async function. Or you could do:

let mappings;
returnMappings(poolec2).then(res => {
    mappings = res;
});

Upvotes: 2

Related Questions