-1

I am using Google cloud api to add companies to Google Talent Solution. They are return data through a promise. I need to return the response.name value when the promise is resolved. But the value that gets return from the .then() method is:

Promise { response.name }

How can I get the response.name value out of the promise so I can actually use it.

createGoogleCompany: function(projectId, tenantId, displayName, externalId) {

  const client = new talent.CompanyServiceClient();
  // const projectId = 'Your Google Cloud Project ID';
  // const tenantId = 'Your Tenant ID (using tenancy is optional)';
  // const displayName = 'My Company Name';
  // const externalId = 'Identifier of this company in my system';
  const formattedParent = client.tenantPath(projectId, tenantId);
  const company = {
    displayName: displayName,
    externalId: externalId,
  };
  const request = {
    parent: formattedParent,
    company: company,
  };
  return promise = client.createCompany(request)
    .then(responses => {
      const response = responses[0];
      console.log(`Created Company`);
      console.log(`Name: ${response.name}`);
      console.log(`Display Name: ${response.displayName}`);
      console.log(`External ID: ${response.externalId}`);
      return response.name
    })
    .catch(err => {
      console.error(err);
    });
},

var nameValue = createGoogleCompany(data,data2,data3,data4)createGoogleCompany(data).then((nameValue) => { console.log("Name Value", nameValue); });

^^ this still returns a promise object that is pending.

T_Dourg
  • 9
  • 2

1 Answers1

-1

Your returning a promise from your function, so you need to get it in the then clause of the promise on the outside.

createGoogleCompany(data).then((nameValue) => {
    console.log("Name Value", nameValue);
});

If async functions available to you could write a function like so

const someFunction = async () => {
   const name = await createGoogleCompany(data);
}

Async functions are just promises underneath but they make things look a little more synchronous

Mirrorcell
  • 384
  • 2
  • 10
  • name = createGoogleCompany(data).then((nameValue) => { console.log("Name Value", nameValue); }); console.log(Id); This ^^ returns 'Promise { }' and then i console.log it later when I know it is finished and it comes back as 'Promise {}'.... Is this what you meant or am i still misunderstanding – T_Dourg Jun 21 '19 at 21:48