someuser2491
someuser2491

Reputation: 1968

How to access a property of an object which is returned from a function using javascript?

i have a method which returns an object. How do i retrieve the property of that object

below is my code,

const called_method = async (context, user) => {
    const available = await context.dbHandler({
        sql:
           'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
           values: { id: user[0].comp_id },
    });
    return available[0];
};


const some_method = () => {
    const available = called_method(context, user);
    const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
   }
}

How can i return first and second from the called method and access it in some_method.

could soemone help me with this. thanks.

Upvotes: 1

Views: 47

Answers (3)

Hamza Arshad
Hamza Arshad

Reputation: 846

You have to wait for the async function:

const called_method = async (context, user) => {
const available = await context.dbHandler({
    sql:
       'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
       values: { id: user[0].comp_id },
});
return available[0];
};


const some_method = () => {
    called_method(context, user).then(function(available) {
      const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
      }
  }
}

Or you can also make your some method async and await for called_method:

const called_method = async (context, user) => {
const available = await context.dbHandler({
    sql:
       'SELECT SUM(first) AS first, SUM(second) AS second FROM items;',
       values: { id: user[0].comp_id },
});
return available[0];
};


const some_method = async () => {
    const available = await called_method(context, user);
    const data = {
        first: available.first, //here i get error says first doesnt exist on type promise
        second: available.second, //here i get error says first doesnt exist on type promise
    }
}

Upvotes: 1

Nicholas Tower
Nicholas Tower

Reputation: 84912

To interact with the eventual value of a promise, you either need to use it's .then method:

const some_method = () => {
  called_method(context, user)
    .then(available => {
      const data = {
        first: available.first,
        second: available.second,
      }
    });
}

Or you need to put your code in an async function and await the promise

const some_method = async () => {
  const available = await called_method(context, user);
  const data = {
     first: available.first,
     second: available.second,
  }
}

Upvotes: 2

mateleco
mateleco

Reputation: 589

Async functions always return a promise, therefore if you want to get its value you have to wait until it settles. You can set some_method as an asynchronous function as well:

const some_method = async () => {
    const available = await called_method(context, user); // wait until the promise settles
    const data = {
        first: available.first, 
        second: available.second
   }
}

Upvotes: 3

Related Questions