Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question