JSFiddle - React, Tailwind, and code Playground
JavaScript
/* Juste une fonction simulant un évènement asynchrone */
function timeOut(self) {
setTimeout(function() {
self.running = true;
console.log('login "' + thisInst.id + '"');
// On "resoud l'objet de login associé à l'objet principal */
self.deferLogin.resolve();
}, 1000);
}
/* objet de test */
unObjet = {
login: function(id) {
thisInst = this;
jQuery.when(thisInst.deferSession).then(function() {
thisInst.id = id;
thisInst.deferLogin = $.Deferred();
thisInst.deferSession = $.Deferred();
timeOut(thisInst);
});
},
state: function() {
thisInst = this;
$.when(this.deferLogin).then(function() {
console.log('Etat de l\'objet "' + thisInst.id + '" : ' + thisInst.running);
});
},
logout: function() {
thisInst = this;
$.when(this.deferLogin).then(function() {
console.log('logout "' + thisInst.id + '"');
thisInst.running = false;
thisInst.deferSession.resolve();
});
}
};
console.log('Première instance : ' + unObjet.running);
unObjet.login(1);
unObjet.state();
unObjet.logout();
console.log('Deuxième instance : ' + unObjet.running);
unObjet.login(2);
unObjet.state();
unObjet.logout();