Async Issue
by clemmansonsss
Babel + JSX
// Server BDD
const people = [
{name: 'John Doe', age: '30'},
{name: 'Jane Doe', age: '24'}
];
// FRONT
const simulateAJAXCall = (index, cb) => {
let chance = Math.random();
let isSuccess= (chance <= 0.80) ? true : false;
return setTimeout(() => {
if (isSuccess) {
return cb(people[index]);
}
else {
return cb(null);
}
}, 1000)
}
const getPerson = (index) => {
return new Promise((resolve, reject) => {
console.log('fetching person...');
simulateAJAXCall(index, (data) => {
if (data === null) {
reject('Something went wrong!');
}
resolve(data);
});
});
}
getPerson(1).then(person => {
console.log(person);
}).catch(error => {
console.log(error);
});
class LeavecalendarController{
constructor(){
}
init(){
loadTemplate()
}
async loadTemplate(){
const response = await (() => {
});
}
}
// 4YOU
leavecalendarController = new LeavecalendarController();
leavecalendarController.init();