Created
May 29, 2019 05:11
-
-
Save saravanan10393/d9408d6a8a1bacd19ba909b52d52786d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ServantA() { | |
//for simplicity hidden the implementation | |
function cleaningHall() { | |
return new Promise(cleaningHallPromiseHandler); | |
} | |
return { | |
cleaningHall: cleaningHall | |
}; | |
} | |
function servantB() { | |
//for simplicity hidden the implementation | |
function arrangeTables() { | |
return new Promise(arrangeTablesPromiseHandler); | |
} | |
return { | |
arrangeTables: arrangeTables | |
}; | |
} | |
function servantC() { | |
//for simplicity hidden the implementation | |
function prepareLunch() { | |
return new Promise(prepareLunchPromiseHandler); | |
} | |
return { | |
prepareLunch: prepareLunch | |
}; | |
} | |
//Three task at time | |
function King() { | |
let servantA = ServantA(); | |
let servantB = ServantB(); | |
let servantC = ServantC(); | |
//King has to keep track up servants work status since each servants works in parallel | |
let isHallCleaned = false; | |
let isTablesArranged = false; | |
let isLunchReady = false; | |
function canBeginCouncil() { | |
if(isHallCleaned === true && | |
isTablesArranged === true && | |
isLunchReady === true) { | |
//Begin the council | |
} | |
} | |
servantA.cleaningHall() | |
.then(function promiseFulfilled() { | |
isHallCleaned = true; | |
//Since servants are working independently there is no order in finishing work so | |
//King has to check overall progress when ever each servant completes his/her work | |
canBeginCouncil(); | |
}) | |
.catch(function promiseFailed(reason) { | |
isHallCleaned = false; | |
}); | |
servantB.arrangeTables() | |
.then(function promiseFulfilled() { | |
isTablesArranged = true; | |
canBeginCouncil(); | |
}) | |
.catch(function promiseFailed(reason) { | |
isTablesArranged = false; | |
}); | |
servantC.prepareLunch() | |
.then(function promiseFulfilled() { | |
isLunchReady = true; | |
canBeginCouncil(); | |
}) | |
.catch(function promiseFailed(reason) { | |
isLunchReady = false; | |
}); | |
} | |
//Wake up the King | |
King(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment