0

General Functionality

async function fname(var1, var2) {
  var store = await new Promise(async (resolve, reject) {
      //.....read function......
      //return the value;
      return data
  });

  console.log("output", store) //"has the required data 

  return store; //returning Promise Pennding
}

I can't return the stored value and it is returning Promise pending

Harshana
  • 5,151
  • 1
  • 17
  • 27
Bcoder
  • 5
  • 2

4 Answers4

1

How you would do it is with the resolve(data) method. You can read more about it here: https://www.geeksforgeeks.org/javascript-promise-resolve-method/ and here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise.

The code example would be:

async function fname(var1, var2) {
  var store = await new Promise(async (resolve, reject) {
      //.....read function......
      //return the value;

      resolve(data)
  });

  console.log("output", store) // has the required data 

  return store; //returning Promise Pending
}

Don't hesitate to comment if you have any questions. :) Happy coding!

they-them
  • 61
  • 5
1

You need to resolve the data

var store = await new Promise(async (resolve, reject) {
        resolve(data); //Returns the data
  });
Gab Agoncillo
  • 77
  • 1
  • 9
0

a Promise returns data by calling it's resolve method with the data, so instead of return data

resolve(data)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise

Rami Jarrar
  • 4,523
  • 7
  • 36
  • 52
0

You could use resolve instead of return in promise

async function fname(var1, var2) {
  var store = await new Promise(async (resolve, reject) {
      //.....read function......
      //return the value;
      resolve(data)
  });

  console.log("output", store) //"has the required data 

  return store; //returning Promise Pennding
}
prasanth
  • 22,145
  • 4
  • 29
  • 53