Currently, I am utilizing Tabletop.js to extract data from my Google Spreadsheet. Within the function, I have initiated a Promise. However, I am facing difficulty extracting the Array data out of the function.
The code snippet is as follows:
function getData() {
return new Promise((resolve) => {
Tabletop.init({key: publicSpreadsheetUrl, callback: showInfo, simpleSheet: true})
resolve('Done');
})
}
let arrayWithData = [];
function showInfo (data, tabletop) {
console.log('showInfo active');
arrayWithData.push(...data);
return new Promise(resolve => {
console.log(arrayWithData, 'data is here')
resolve(arrayWithData) // This doesn't work yet
})
}
showInfo().then(data => {
console.log(data, 'data from the Promise')
}) // This doesn't work
My intention is to utilize this Array later on in React components.
Edit
By using Keith's snippet, I managed to make my code functional and also incorporated a reject
handler within my Promise of getData() sourced from the MDN site.
Promise.reject(new Error('fail')).then(function() {
// not called
}, function(error) {
console.log(error); // Stacktrace
});
The only hurdle now is comprehending the error triggered by my Promise.reject
. It displays the following error:
Error: fail
at eval (eval at hmrApply (base.eaab6c8c.js:297), <anonymous>:37:20)
at new Promise (<anonymous>)
at getData (eval at hmrApply (base.eaab6c8c.js:297), <anonymous>:30:10)
at Object.eval (eval at hmrApply (base.eaab6c8c.js:297), <anonymous>:63:1)
at newRequire (script.726c79f3.js:48)
at hmrAccept (base.eaab6c8c.js:328)
at base.eaab6c8c.js:214
at Array.forEach (<anonymous>)
at WebSocket.ws.onmessage (base.eaab6c8c.js:212)