admin管理员组文章数量:1326140
In some libraries/frameworks documentations, it tells you that you can use synchronous or asynchronous function.
For example, in Mongoose documentation it says:
Custom validators can also be asynchronous. If your validator function takes 2 arguments, mongoose will assume the 2nd argument is a callback.
So basically when you define a function like this:
function(a){
return false;
}
Mongoose will consider it as synchronous, but we define it like this:
function(a,callback){
setTimeout(function(){
callback(false);
},5000)
}
It will be taken as asynchronous code.
I've noticed the same thing with Mocha testing framework, in the documentation is says:
Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is plete. By adding a callback (usually named done) to it(), Mocha will know that it should wait for this function to be called to plete the test.
My question is: How do they do this? How would you know when calling a function whether it takes 1 or 2 arguments?
In some libraries/frameworks documentations, it tells you that you can use synchronous or asynchronous function.
For example, in Mongoose documentation it says:
Custom validators can also be asynchronous. If your validator function takes 2 arguments, mongoose will assume the 2nd argument is a callback.
So basically when you define a function like this:
function(a){
return false;
}
Mongoose will consider it as synchronous, but we define it like this:
function(a,callback){
setTimeout(function(){
callback(false);
},5000)
}
It will be taken as asynchronous code.
I've noticed the same thing with Mocha testing framework, in the documentation is says:
Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is plete. By adding a callback (usually named done) to it(), Mocha will know that it should wait for this function to be called to plete the test.
My question is: How do they do this? How would you know when calling a function whether it takes 1 or 2 arguments?
Share Improve this question asked Nov 16, 2016 at 11:46 Alex C.Alex C. 4,0814 gold badges22 silver badges25 bronze badges3 Answers
Reset to default 7You'd just check the arguments
function something(arg1, arg2) {
if (typeof arg2 === 'function') {
console.log(arg1 + ' has callback');
} else {
console.log(arg1 + ' does not have callback');
}
}
something('call 1'); // no callback
something('call 2', function() {}); // has callback
To check how many arguments a function is expecting, you'd use Function.length
length
is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number excludes the rest parameter and only includes parameters before the first one with a default value. By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function.
console.log((function() {}).length); /* 0 */
console.log((function(a) {}).length); /* 1 */
console.log((function(a, b) {}).length); /* 2 etc. */
How do they do this? How would you know when calling a function whether it takes 1 or 2 arguments?
You can figure this out using functionname.length
.
function some(a,b,c){
return false;
}
function somethingElse(a){
return false;
}
some.length //returns 3.
somethingElse.length //returns 1.
var func= function(a,b){
}
console.log(func.length);
本文标签: javascriptCheck if a function is using a callback or returning a valueStack Overflow
版权声明:本文标题:javascript - Check if a function is using a callback or returning a value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742189935a2430022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论