Skip to content Skip to sidebar Skip to footer

Get Data From A Promise

I'm working with Tabletop.js to get data from my Google Spreadsheet. In the function, I've invoked a Promise. The only problem is I can't get the data(which is an Array) out of the

Solution 1:

You seem to have a couple of issues here..

Firstly, you have showInfo().then, I'm pretty sure you meant to do -> getData().then(

Your next problem is your getData function,. like @ChrisG said your just resolving a promise instantly here, below is more likely what you meant to do.

function getData() {
  return new Promise((resolve) => {
    Tabletop.init({key: publicSpreadsheetUrl, 
      callback: function (data, tabletop) { resolve(showInfo(data, tabletop)); },
      simpleSheet: true})
  })
}

Lastly your showInfo is not doing anything async so it can be simplified to ->

function showInfo (data, tabletop) {
  console.log('showInfo active');
  arrayWithData.push(...data);
  console.log(arrayWithData, 'data is here')
  return arrayWithData;
}

One last thing, there is no error checking here, normally callbacks have some way to inform you of an error condition, then you could also add the reject handler.


Post a Comment for "Get Data From A Promise"