-1

In the following code:

let getIVoneDay = async (symbol, option, date, exp_date, strike) => {
  let close_ce = await getCE(symbol, option, date, exp_date, strike);
  const close_pe=await getPE(symbol, option, date, exp_date, strike);
  const close_xx= await getXX(symbol, option, date, exp_date, strike);
  console.log(close_ce,close_pe,close_xx);
};

I get the value

undefined undefined undefined

The three functions take some params, make a query in DB and return a value which takes a little time. So, i have tried to use it with async/await and yet i get the same results. How can i do this? I have tried callbacks too for the same and get the same results.

for calling:

var x = getIVoneDay("ACC", "CE", "2020-01-01", "2020-01-30", 1220);

The get functions:

let getPE = (symbol, option, date, exp_date, strike) => {
  var collection_pe = symbol + ".PE";
  var model_pe = mongoose.model("model_pe", bhavcopySchema, collection_pe);
  model_pe
    .find({
      SYMBOL: symbol,
      STRIKE_PR: strike,
      OPTION_TYP: "PE",
      EXPIRY_DT: new Date(exp_date),
      TIMESTAMP: new Date(date),
    })
    .exec((err, result) => {
      let close_pe = result[0].CLOSE.value;
      console.log(close_pe);
      return close_pe;
    });
};

PS: the print inside getCE() and other functions prints the correct value.

  • 1
    how do you call getIVoneDay() ? – Aalexander Jan 13 '21 at 09:40
  • 3
    "*The three functions take some params, make a query in DB and return a value which takes a little time.*" do they return a promise or do they just take a callback? See [How do I convert an existing callback API to promises?](https://stackoverflow.com/q/22519784) although your API might already support an alternative promise interface. – VLAZ Jan 13 '21 at 09:42
  • Seems like you need to go deeper to your functions and check why they are returning undefined. Put logs inside them and check, maybe it is your db returns undefined and the reason not in async/await – Nigrimmist Jan 13 '21 at 09:43
  • @Alex please check the edit. – aditya gaddhyan Jan 13 '21 at 09:48
  • 1
    `getPE` is not returning anything. It should return a `Promise` (see @VLAZ comment) – thedude Jan 13 '21 at 09:53
  • You need to use promises as others said already – Ardahan Kisbet Jan 13 '21 at 09:54

2 Answers2

0

Your function getPE() is actually not an async function. There is no return value.

You Should change it to return a promise like this :

let getPE = (symbol, option, date, exp_date, strike) => {
  var collection_pe = symbol + ".PE";
  var model_pe = mongoose.model("model_pe", bhavcopySchema, collection_pe);
  // Return the promise
  return model_pe
    .find({
      SYMBOL: symbol,
      STRIKE_PR: strike,
      OPTION_TYP: "PE",
      EXPIRY_DT: new Date(exp_date),
      TIMESTAMP: new Date(date),
    })
    .exec((err, result) => {
      let close_pe = result[0].CLOSE.value;
      console.log(close_pe);
      return close_pe;
    });
};
Weedoze
  • 13,683
  • 1
  • 33
  • 63
0

change get functions like following code beacuse it's not a async function:

let getPE = async (symbol, option, date, exp_date, strike) => {
  var collection_pe = symbol + ".PE";
  var model_pe = mongoose.model("model_pe", bhavcopySchema, collection_pe);
  // Return the promise
  try {
    let result = await model_pe.find({
      SYMBOL: symbol,
      STRIKE_PR: strike,
      OPTION_TYP: "PE",
      EXPIRY_DT: new Date(exp_date),
      TIMESTAMP: new Date(date),
    })
    let close_pe = result[0].CLOSE.value;
      console.log(close_pe);
      return close_pe;
  } catch (error) {
    throw error
  }
};
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39