I Have a method in javascript which call an GET api
var PC;
function GetDetails() {
$.ajax({
type: "GET",
url: Myurl,
success: function (response) {
//console.log(response);
PC= response;
}
});
}
I am setting response in a variable named PC and in another method i am calling this
function PerformTask()
{
GetDetails();
console.log(PC);
}
In GetDetails Method console.log(response); works but in PerformTask() console.log(PC) is undefined
As per my understanding it is a async call and PC is not set yet
How can i make it sync with next statement , ?? as i need value of PC to execute next set of statement
I also tried fetch api call
fetch(Myurl)
.then(resp => resp.json())
.then(resp=> setting variable here) ;
But it does not work (work but in async way )
Update 1
return new Promise(function (resolve, reject) {
$.ajax({
type: "GET",
url: Myurl,
success: function (response) {
//console.log(response);;
resolve(response);
},
error: function (err) {
reject(err);
}
});
});
And in Performtask()
GetPropertyDetails()
.then(function (data) {
PC= data;
});
console.log(PC);
But still PC is undefined