Skip to content

Instantly share code, notes, and snippets.

@ronit-mukherjee
Created December 30, 2018 15:08
Show Gist options
  • Save ronit-mukherjee/506fdd0cc1345529c1c3aebb00fa2371 to your computer and use it in GitHub Desktop.
Save ronit-mukherjee/506fdd0cc1345529c1c3aebb00fa2371 to your computer and use it in GitHub Desktop.
function main() {
//Function to fetch user data from the server
function getUserData() {
return new Promise(function(resolve, reject) {
//Set the user data before hand statically in a variable
var userData = [{
name: "Sumit Singh",
age: 28,
email: "sumit@gmail.com",
address: "123 Street, 4th Avenue, New York",
member_since: 2016,
frnds_count: 200
},
{
name: "Amit",
age: 24,
email: "amit@gmail.com",
address: "112 Street, 7th Avenue, New York",
member_since: 2018,
frnds_count: 600
},
{
name: "Ronit",
age: 25,
email: "ronit@gmail.com",
address: "223 Street, 6th Avenue, New York",
member_since: 2017,
frnds_count: 500
},
{
name: "Annu",
age: 22,
email: "annu@gmail.com",
address: "126 Street, 6th Avenue, New York",
member_since: 2016,
frnds_count: 400
},
{
name: "Mita",
age: 17,
email: "mita@gmail.com",
address: "6th Avenue, New York",
member_since: 2018,
frnds_count: 210
}
];
setTimeout(function() {
resolve(userData) //Set promise as successful and now send the data
}, 500); //Emulating Server call
});
}
//Function to hit API to send gift at user's residence
function sendGift() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
//Emulating result of sucess and failure from server
var responseFromServer = 1; //Math.floor(Math.random() * 1) + 0;
if (responseFromServer === 1) { //Success from server
resolve(true); //Mark promise as successful
} else {
reject(false);
}
}, 500); //Emulating server call
});
}
//Function to hit API to send gift at user's residence
function sendMail() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
//Emulating result of sucess and failure from server
var responseFromServer = 1; //Math.floor(Math.random() * 1) + 0;
if (responseFromServer === 1) { //Success from server
resolve(true); //Mark promise as successful
} else {
reject(false);
}
}, 500); //Emulating server call
});
}
//Fetch data of user from the server
console.log("Fetching user data...");
getUserData()
.then(function(users) {
console.log("User Data Fetched Successfully!!!");
for (var i = 0, l = users.length; i < l; i++) {
var user = users[i];
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
(function(u) {
if (user.frnds_count > 500) {
console.log("Calling gift send API for " + u.name + "...");
sendGift(u).then(function(response) {
if (response === true) {
console.log("Sent gift to " + u.name + " at " + u.address);
}
}).catch(function(response) {
console.log("Error: API Failed | Couldn't Sent gift to " + u.name + " at " + u.address);
});
}
if (currentYear - u.member_since > 1) {
console.log("Calling mail send API for " + u.name + "...");
sendMail(u).then(function(response) {
if (response === true) {
console.log("Sent mail to " + u.name + " at " + u.email);
}
}).catch(function(response) {
console.log("Error: API Failed | Couldn't Sent gift to " + u.name + " at " + u.email);
});
}
})(user);
}
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment