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 Serasinghe
  • 4,340
  • 1
  • 11
  • 21
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
  • 65
  • 1
  • 8
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,387
  • 6
  • 33
  • 50
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
  • 21,342
  • 4
  • 27
  • 50