-1

I am getting values from a distant table that I pushed into an array :

function getrecords(base, view) {
    var tab = []; 
    var jsonTab = {};
    base('Table 1').select({
        view: view
    }).eachPage(function page(records, fetchNextPage) {
        records.forEach(function(record) {
            tab.push({
                'Name':record.get('Name'),
                'Notes': record.get('Notes')
            });
        });
        //console.log(tab); 
        return tab;
    });
}

Now, with promiseJs I made a promise function since the call is asynchronous:

function readRecords(base, view){
    return new Promise(function(fulfill, reject){
        getrecords(base, view, function (err, res){
            if(err) reject(err);
            else fulfill(res);
        });
        });
}

Now what I want to do is to use that tab! But when I create a var like that and console log it :

var tabRecord= readRecords(base, view); 
console.log(tabRecord); 

That's the result in cmd:

Promise { _45: 0, _81: 0, _65: null, _54: null }

What does it suppose to mean? Why haven't I the tab shown? What should I do in order to get the return value of my function?

Thank u all.

ps : I helped mysefl with this website https://www.promisejs.org/ to write the promise.

hhelbawi
  • 151
  • 2
  • 12
  • You're free to call your variables anything you want, of course, but *overwhelmingly*, the names used for the arguments given to the promise constructor callback are typically `resolve` and `reject`. – T.J. Crowder Oct 06 '16 at 14:55
  • 1
    `getrecords` should either call a callback or return a promise. Right now it does neither. – Wex Oct 07 '16 at 13:30

2 Answers2

2

You use the argument to the callback you pass then:

readRecords(base, view).then(function(tabRecord) {
    // Use `tabRecord` here
});

Promises don't make asynchronous code synchronous. They just make asynchronous code easier to work with.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I understand more now with your last sentence. But when I pass then like that : readRecords(base, view).then(function(res){ console.log(res);} nothing happens in the console. I will investigate further. – hhelbawi Oct 06 '16 at 15:23
  • Hello, I am maybe missing a point here... Nothing is shown in the command prompt.. when I write a console log in the condition if(err) or in the else, nothing is shown either. But when I put a console log in the getrecords method I have the tab. So basically when the function readRecords reaches getrecords it shows on the prompt the result of the log and that's it. It does not reach the reject or fulfill..Do I have to Ask a new question about that? I here in the comments it's ok? Thank you. – hhelbawi Oct 07 '16 at 09:37
  • 1
    @hhelbawi: If you're expecting your `getrecords` to *return* the information from the asynchronous calls, it can't. See [*How do I return the response from an asynchronous call?*](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – T.J. Crowder Oct 07 '16 at 09:39
0

Getrecords should return a promise so I did:

function getrecords(base, view) {
    return new Promise(function(resolve, reject){
        var tab = []; 
        base('Table 1').select({
            view: view
        }).eachPage(function page(records, fetchNextPage) {
            records.forEach(function(record) {
            //resolve(records);
                tab.push({
                    'Name':record.get('Name'),
                    'Notes': record.get('Notes')
                });
            });
            fetchNextPage();
            resolve(tab);
            reject("Nononono");
        });
    });
}

After that I can call the method followed by "then" like this:

getrecords(base, view).then(function(res){return res;}).then(function(res{tabToCSV(res)})

It worked.

hhelbawi
  • 151
  • 2
  • 12