在类javascript类构造函数中使用异步调用

时间:2019-08-01 03:09:18

标签: javascript class javascript-objects

我正试图用Javascript创建一个类来完成所有的Google工作表功能。在编写任何函数之前,我在构造函数中调用了authenticate API,但是在调用实际函数之前,身份验证尚未完成。这是类文件中的代码,

const { google } = require("googleapis")
const authentication = require("./authentication");

class Sheets {
    constructor() {
        this.test = 'stackoverflow';
        authentication.authenticate().then((auth)=>{
            this.auth = auth;
        });
    }

    async writeToSheet(spreadsheetId, range, data) {
        if(typeof spreadsheetId != "undefined") {
            console.log(this.test)
            console.log('Got the spreadsheetId:' + spreadsheetId)
            console.log('Got the auth:' + this.auth)
            return;    
        }

        var sheets = google.sheets('v4');
        await sheets.spreadsheets.values.update({
            auth: this.auth,
            spreadsheetId: spreadsheetId,
            range: range, //Change Sheet1 if your worksheet's name is something else
            valueInputOption: "USER_ENTERED",
            resource: {
                values: data
            }
        }, (err, response) => {
            if (err) {
            console.log('The API returned an error: ' + err);
            return;
            } else {
                console.log("Data updated to the sheet successfully!");
            }
        });
    } 
}

module.exports = Sheets;

这就是我调用函数的方式

let sheets = await new Sheets();
sheets.writeToSheet(spreadSheetId, range, data);

当我如上所述调用writeToSheet函数时,总是像这样仅将auth打印为undefined,

stackoverflow
Got the spreadsheetId:9ijWvEeWG7Oybv_TJi5AMkjl18dFHMFcBfSfkEG24-p7
Got the auth:undefined

那么,如何在调用类中的任何其他函数并同步设置this.auth之前调用authenticate函数?任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:2)

我认为您只需要创建一个额外的工作流类型函数即可处理身份验证部分,然后将其传递给writeToSheet拥有的逻辑;像这样的东西:

const { google } = require("googleapis")
const authentication = require("./authentication");

class Sheets {
    async authenticate() {
        const auth = await authentication.authenticate();
        this.auth = auth;
    }

    async writeToSheet(spreadsheetId, range, data) {
        await this.authenticate();
        await _writeToSheet(spreadsheetId, range, data);
    } 

    async _writeToSheet(spreadsheetId, range, data){
        if(typeof spreadsheetId != "undefined") {
            console.log('Got the spreadsheetId:' + spreadsheetId)
            console.log('Got the auth:' + this.auth)
            return;    
        }

        var sheets = google.sheets('v4');
        await sheets.spreadsheets.values.update({
            auth: this.auth,
            spreadsheetId: spreadsheetId,
            range: range, //Change Sheet1 if your worksheet's name is something else
            valueInputOption: "USER_ENTERED",
            resource: {
                values: data
            }
        }, (err, response) => {
            if (err) {
            console.log('The API returned an error: ' + err);
            return;
            } else {
                console.log("Data updated to the sheet successfully!");
            }
        });
    }
}

module.exports = Sheets;

这会稍微改变您对函数的使用方式:

let sheets = new Sheets();
await sheets.writeToSheet(spreadSheetId, range, data);

您可以将其中一些添加到constructor中,但是请注意,您存储的是先前已认证的类实例。如果您存储实例并稍后尝试使用已过期的身份验证令牌重用它,则可能会出现问题。