E
E
Eugene2019-08-20 20:48:11
JavaScript
Eugene, 2019-08-20 20:48:11

How to implement module export after executing asynchronous function?

I'm using the google-spreadsheet spreadsheet library and I want to export some functions to another module. The problem is that authorization must be performed before using the functions.

const GoogleSpreadsheet = require('google-spreadsheet');
const { promisify } = require('util');
const doc = new GoogleSpreadsheet(options.spreadSheetId);

// Авторизация
await promisify(doc.useServiceAccountAuth)(creds);
const info = await promisify(doc.getInfo)();

// Функции которые нужно экспортировать
let sheet = info.worksheets[0];
await promisify(sheet.addRow)(user_data);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FabiBoom, 2019-08-20
@you_are_enot

Promise can be exported

module.exports = async function() {
      const exportFunctions = {};
       //  код

      // добавляем функции нужные
      exportFunctions.sheet = sheet;
       
      return exportFunctions;
   };

  // другой файл

  const mod = require('./test.js').then(funcs => console.log(funcs)); // наши функции

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question