Permalink
Browse files

Adds demo code

  • Loading branch information...
kbbushman
kbbushman committed Jan 13, 2020
0 parents commit 9df948bbd3335f1c0a741cb43426a4aec3c6d90e
Showing with 132 additions and 0 deletions.
  1. +132 −0 app.js
132 app.js
@@ -0,0 +1,132 @@
const users = [];

console.log('Users starting value \n', users);
console.log(`---------------------------------------`);

setTimeout(() => {
console.log('Adding new user');
users.push({id: 1, name: 'John Doe'});
console.log('New user added');
console.log(`---------------------------------------`);
console.log('Users after push \n', users);
console.log(`---------------------------------------`);
}, 2000);

console.log('New Users Last Check \n', users);
console.log(`---------------------------------------`);


// -------------------- FIX WITH CALLBACK ------------------- //

console.log('Users starting value \n', users);
console.log(`---------------------------------------`);

const addUser = (user, callback) => {
setTimeout(() => {
console.log('Adding new user');
users.push(user);
console.log('New user added');
console.log(`---------------------------------------`);
console.log('Users after push \n', users);
console.log(`---------------------------------------`);
callback();
}, 2000);
}

addUser({id: 1, name: 'John Doe'}, () => {
console.log('New Users Last Check \n', users);
console.log(`---------------------------------------`);
});



// ------------------------------------- ADVANCED CALLBACK & PROMISE ------------------------------ //


function customAjax(url, configObj) {
console.log('Calling API...');
const { method, headers, data, success, error } = configObj;
const database = [];

return new Promise((resolve, reject) => {
if (typeof data !== 'object' || Array.isArray(data)) {
if (error) return error(new TypeError('Data must be an object'));
reject(new TypeError('Data must be an object'));
} else if (!data) {
if (error) return error(new ReferenceError('Data is undefined'));
reject(new ReferenceError('Data is undefined'));
}

setTimeout(() => {
const randomNum = Math.floor((Math.random() * 10000) + 1) // 1-1000
data.id = randomNum * randomNum;
database.push(data);
if (success) return success(data);
resolve(data);
}, 3000);
});
}


const newUser = {
name: 'John Doe',
email: 'jdoe@gmail.com',
};

customAjax('http://someapi.com/users', {
method: 'POST',
headers: {},
data: newUser,
success: (savedUser) => {
console.log('Success', savedUser);
},
error: (err) => {
console.log(err);
}
});


customAjax('http://someapi.com/users', {
method: 'GET',
headers: {},
data: newUser,
})
.then((res) => console.log(res))
.then(() => console.log('All Done'))
.then(() => console.log('Make another api call'))
.then(() => {
customAjax('',{data: newUser})
.then((res) => console.log('Second Response Result\n', res))
})
.catch((err) => console.log(err));


console.log('Users before createUser \n', users);

const createUser = async (user) => {
try {
const savedUser = await customAjax('http://someapi.com/users', {
method: 'GET',
headers: {},
data: newUser,
});

console.log(savedUser);
users.push(savedUser);
console.log('Part 2')
const savedUser2 = await customAjax('http://someapi.com/users', {
method: 'GET',
headers: {},
data: newUser,
});

console.log(savedUser2);
users.push(savedUser2);
} catch (error) {
console.log('In the Error block \n', error);
}
};

createUser(newUser);

console.log('Users after createUser \n', users);

0 comments on commit 9df948b

Please sign in to comment.