const Controller = function(){
var busy = false;
return {
ready: () => busy === false,
do: (data) => new Promise((resolve,reject) => {
if (busy)
reject("I`m busy dude!")
busy = true;
// +100500 code rows
setTimeout(() => {
busy = false;
resolve("wow, its working fine!");
}, 1000);
})
}
}
let ctrl = new Controller();
ctrl.do("something")
.then(result => console.log("Job's done: " + result))
ctrl.do("fault")
.catch(err => console.log(`Houston, we have a problem: ${err}`));
setTimeout(() => ctrl.do("something again").then(result => console.log("It works again: " + result)), 1100)
P.S. делайте очередь, с промисами это несколько строк кода