hey people I'm facing an issue and I can't find a solution. I'm trying to chain 2 rest call functions and after them use a function which uses the objects returned from both of them. For some reason something is not working I think its something to do with the syntax I wrote. I would like to understand why my code:
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script>
var users = [];
var user = null;
function restCallA() {
$.ajax({
url: "https://reqres.in/api/users?page=2",
type: "GET",
success: function(response) {
users = response.data;
}
});
}
function restCallB() {
$.ajax({
url: "https://reqres.in/api/users/2",
type: "GET",
success: function(response) {
user = response.data;
}
});
}
function myFun() {
users.push(user);
console.log(users);
}
restCallA()
.then(restCallB())
.then(myFun());
</script>
</body>
</html>
error:
test.html:38 Uncaught TypeError: Cannot read property 'then' of undefined