2

I have an async function, which I want to return a promise. If I wrap it inside a promise, it says something like await has to be inside async function.

async function accessSpreadsheet(first_name,last_name,email,phone) {
  await doc.useServiceAccountAuth({
    client_email: creds.client_email,
    private_key: creds.private_key,
  });

  await doc.loadInfo(); // loads document properties and worksheets

  const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id]
  console.log(sheet.title);
  await sheet.addRow({ "first name": first_name,"last name": last_name, email: email,phone:phone });
}
TheMaster
  • 45,448
  • 6
  • 62
  • 85
Muhammad Naufil
  • 2,420
  • 2
  • 17
  • 48
  • 6
    async functions always return a promise. Even if you return something that is not a promise, it will be implicitly wrapped in a promise and then returned. – Yousaf Aug 12 '20 at 12:36
  • 2
    The return value of an `async function` is a `Promise`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function – goto Aug 12 '20 at 12:37
  • It doesnt console.log(sheet.title). What do I do – Muhammad Naufil Aug 12 '20 at 12:40
  • What does `doc.sheetsByIndex[0]` returns? – Yousaf Aug 12 '20 at 12:43
  • an excel sheet object, which has details of sheet title and sheet rows – Muhammad Naufil Aug 12 '20 at 12:45
  • Why do you think wrapping an async function with the promise will solve your problem? Your problem is somewhere else. Try wrapping the code in your function in `try-catch` block and see if there are any errors thrown during the execution of this function. – Yousaf Aug 12 '20 at 13:02
  • 1
    @player0 Questions tagged nodejs or python or any other language should NOT be tagged with [tag:google-apps-script]( uses Internal api). They should be tagged with [tag:google-sheets-api](external api). See [tag info page](https://stackoverflow.com/tags/google-sheets-api/info) for more details. – TheMaster Aug 12 '20 at 14:12

0 Answers0