The following ajax call that I'm handinglind with a promise
function hit_db()
{
return new Promise(function (resolve, reject)
{
$.ajax({
url: target_url,
type: "GET",
async: true,
crossDomain: true,
success: function (data) { //<--Ultimately I want this snippet to return this data variable
resolve(data);
},
error: function (err) {
reject(error);
}
});
});
}
return hit_db().then(data => {. // <--EDIT
return data;
});
Whenever I try to return data I get obj Promise
instead of the server response which is supposed to say Great! Success!
I thought that once the promised was fulfilled I could return data which is the server response.
Here i'm taking about how can I unwrap the Promise object from the async response that I'm getting
Then using the async/await syntax stills returns an obj Promise
function hit_db()
{
return new Promise(function (resolve, reject)
{
$.ajax({
url: target_url,
type: "GET",
async: true,
crossDomain: true,
success: function (data) {
resolve(data);
},
error: function (err) {
reject(error);
}
});
});
}
async function handler() {
try {
var data = await hit_db();
return data;
}
catch (error) {
throw new Error('Network call failed');
}
}
return handler();
Side Note: I have already solved any possible CORS issues with my ajax call