简体   繁体   English

检查函数是使用回调还是返回值

[英]Check if a function is using a callback or returning a value

In some libraries/frameworks documentations, it tells you that you can use synchronous or asynchronous function. 在某些库/框架文档中,它告诉您可以使用同步或异步功能。

For example, in Mongoose documentation it says: 例如,在Mongoose文档中它说:

Custom validators can also be asynchronous. 自定义验证器也可以是异步的。 If your validator function takes 2 arguments, mongoose will assume the 2nd argument is a callback. 如果验证器函数有2个参数,mongoose将假设第二个参数是回调。

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: Mongoose会将其视为同步,但我们将其定义为:

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: 我已经注意到Mocha测试框架的相同之处 ,在文档中说:

Testing asynchronous code with Mocha could not be simpler! 使用Mocha测试异步代码并不简单! Simply invoke the callback when your test is complete. 只需在测试完成后调用回调。 By adding a callback (usually named done) to it(), Mocha will know that it should wait for this function to be called to complete the test. 通过向it()添加一个回调(通常名为done),Mocha将知道它应该等待调用此函数来完成测试。


My question is: How do they do this? 我的问题是: 他们如何做到这一点的? How would you know when calling a function whether it takes 1 or 2 arguments? 你怎么知道在调用函数时是否需要1或2个参数?

You'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 要检查函数期望的参数数量,可以使用Function.length

length is a property of a function object, and indicates how many arguments the function expects, ie the number of formal parameters. length是函数对象的属性,表示函数期望的参数数量,即形式参数的数量。 This number excludes the rest parameter and only includes parameters before the first one with a default value. 此数字不包括rest参数,仅包含具有默认值的第一个参数之前的参数。 By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function. 相比之下,arguments.length是函数的本地,并提供实际传递给函数的参数数量。

 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? 你怎么知道在调用函数时是否需要1或2个参数?

You can figure this out using functionname.length . 你可以使用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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM