How to get a return from a foreground nodejs

Asked

Viewed 250 times

1

I can’t figure out how to get back a Password in Javascript. In the example below, how to get the return of const info?

const GoogleSpreadSheet = require('google-spreadsheet');
const credentials = require('../credenciais.json');
const { promisify } = require('util');

const acessoPlanilha = async() => {
    const planilha = new GoogleSpreadSheet(planilhaID);
    await promisify(planilha.useServiceAccountAuth)(credentials); 
    const info = await promisify(planilha.getInfo)();
    return info;
}

const info = acessoPlanilha();
console.log(info);


//terminal: Promise { <pending> }

1 answer

1

Since the rest of the code is correct, you just have to wait for the promise to come true. When it fulfills its code it will be called back both in the case of a success, first parameter, and in the case of a failure, second parameter.

The documentation, referred to below, explains in the following way:

p.then(quandoRealizada, quandoRejeitada);

So in your case, it can be done like this:

info.then(result => console.log(result), err => console.error(err));

This is the link of the documentation. That one page has several examples of how to use.

  • Would that be the code? Let result; const info = accessPlanilha(); info.then(result => result = result, err => console.log(err)); console.log(info); console.log(result);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.