Last active
August 29, 2015 14:24
-
-
Save mohayonao/fda099f46099a24be5af to your computer and use it in GitHub Desktop.
This file contains hidden or 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 create(object) { | |
| var wait = Promise.resolve(); | |
| function synchronized(func) { | |
| function next() { | |
| return func(object); | |
| } | |
| return (wait = wait.then(next, next)); | |
| } | |
| return synchronized; | |
| } | |
| module.exports = { | |
| create: create, | |
| }; |
This file contains hidden or 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
| var synchronizer = require("./synchronizer"); | |
| var locked = false; | |
| function asyncFuncNeedsExclusiveAccessControl(message) { | |
| if (locked) { | |
| console.error("FATAL ERROR: LOCKED!!"); | |
| return process.exit(1); | |
| } | |
| locked = true; | |
| return new Promise(function(resolve, reject) { | |
| setTimeout(function() { | |
| console.log(message + "!!"); | |
| locked = false; | |
| resolve(message + ": OK"); | |
| }, 100); | |
| }); | |
| } | |
| var synchronized = synchronizer.create(); | |
| synchronized(function() { | |
| return asyncFuncNeedsExclusiveAccessControl("foo"); | |
| }).then(function(res) { | |
| console.log("-->", res); | |
| }); | |
| synchronized(function() { | |
| return asyncFuncNeedsExclusiveAccessControl("bar"); | |
| }).then(function(res) { | |
| console.log("-->", res); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment