javascript - How to implement mocha like optional async callbacks? -
I would like to apply alternative async callback in the same way as the moach.
I mean I have a callback function which can be both sync and async.
When the user uses the optional callback parameter 'done', then it is an ASC when there is no synchronous callback parameter.
// Sync version ('Something syncs', function () {console.log ('this is the sync version')}); // async version (some async does, function (done) {setTimeout (function () (done (true);}, 1000);});Whether the callback synchronization or asyncci, How can this differ?
How to implement any idea?
If you can assume that your function has not been overridden to
toString , then you can use it on user-defined functions to check if it has
done not G is an argument, for example:
function argIndexOf (f, arg) {var s = f + '; return s.sbrstring (s.indexOf (' (') + 1 , S.indexOf (')')) .replace (/ \ s + / g, '') .split (','). IndexOf (arg);} ArgIndexOf (function () {}, 'done'); // 1 argIndexOf (function (done) {}, 'done') // 0 argIndexOf (function (arg1, done) {}, 'done') // 1
that function Will act as an argument in a function and return its status to
done logic, or if it does not use any one, you can use IE8 or Use in earlier versions Want. If you have tagged question
node.js , then I think that this is not a problem.
You can also add it to the function. Prototype, however it is highly disappointed that you want your code to play well with others:
function .prototypeindexoff = function.prototype.index || Function (arg) {var s = this + ''; Return s.substring (s.indexOf ('(') + 1, s.indexOf (')') .replace (/ \ s + / g, '') .split (','). IndexOf (arg); };
Then you can just use it:
func.indexOf ('done')
Can also be used and it will ignore negative indexes, so call the callback and pass the value to 'done':
var args = [] ; Args [Funny. Index ('done')] = made key; Func.apply (func, args);
Comments
Post a Comment