Created
June 14, 2015 16:24
-
-
Save oimou/b86ca11557bdc0aa7a11 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
"use strict"; | |
/** | |
* decorator to promisify instance methods | |
* | |
* @param {Object} target | |
* @param {String} name | |
* @param {Object} descriptor | |
* @return {Object} | |
*/ | |
function promisified(target, name, descriptor) { | |
let original = descriptor.value; | |
descriptor.value = function (...args) { | |
return new Promise((resolve, reject) => { | |
let then = (err, res) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(res); | |
}; | |
original.apply(this, args.concat(then)); | |
}); | |
}; | |
return descriptor; | |
} | |
/** | |
* Gene | |
* | |
* @class Gene | |
*/ | |
class Gene { | |
/** | |
* constructor | |
* | |
* @param {String} code | |
*/ | |
constructor(code = "") { | |
this.code = code; | |
} | |
/** | |
* set random chromosomes | |
* | |
* @param {Number} length | |
* @param {Function} done | |
*/ | |
@promisified | |
random(length, done) { | |
while (length--) { | |
this.code += String.fromCharCode( | |
Math.floor(Math.random() * 255) | |
); | |
} | |
done(); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment