I'm implementing Promises for the first time in JS and am getting uncaught in promise exception in console log while running the below code.
function data_present() {
return new Promise((resolve, reject) => {
fetch(api)
.then(response => response.json())
.then(message => {
console.log(message)
if(message && Object.keys(message).length != 0) {
resolve()
}
else {
reject()
}
})
})
}
I am handling the result of the promise return value in the main function as below and am yet getting the uncaught in promise message:
function main() {
data_present().then(() => {
load_graph()
}).catch(() => {
data_present()
})
}
The logic behind the data_present() is to wait till we get a non-empty JSON response at the API endpoint and keep polling it if JSON response is empty.
The exception I'm getting is as follows:
Uncaught (in promise) undefined
(anonymous) @ index.js:34
Promise.then (async)
(anonymous) @ index.js:29
data_present @ index.js:26
(anonymous) @ index.js:56
Promise.catch (async)
getParametersData @ index.js:55
onclick @ (index):92