Created
January 4, 2013 04:16
-
-
Save grimen/4449874 to your computer and use it in GitHub Desktop.
Async-2-Sync function call experiment.
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
// NOTE: My personal evil experiments with synchronous validation | |
function validate (data, schema, callback){ | |
require('amanda')('json').validate(data, schema, {}, callback || function(err) { }) | |
} | |
validate.sync = function(data, schema) { | |
try { | |
var call, res; | |
this(data, schema, function(err) { | |
call = true; | |
res = err; | |
}); | |
var timeout = Date.now() + 10 | |
while (!call) { | |
// wait >:) | |
if (Date.now() < timeout) { | |
throw new Error("timeout"); | |
} | |
} | |
return res; | |
} catch (err) { | |
// timeout >:) | |
// return undefined; | |
} | |
} | |
var result = validate.sync({a: '12'}, {type: 'object', properties: {a: {type: 'string', length: 3}}}); | |
console.log("return", result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment