简体   繁体   English

回调返回值是否有意义?

[英]Does it make sense for a callback to return a value?

Commonly, an async function in js returns a promise wrapping either the resolved value or error message. 通常,js中的异步函数会返回包装已解析值或错误消息的Promise。 If promise style do not apply, a callback can be passed as a parameter to that async function, in which the result of the async function is accessible. 如果不应用Promise样式,则可以将回调作为参数传递给该异步函数,从中可以访问异步函数的结果。

Just wonder if it is meaningful for such a callback to have ar eturn statement? 只想知道这样的回调是否具有ar eturn语句是否有意义? If so, who can receive the returned value and is there any useful use case? 如果是这样,谁可以接收返回的值,并且有任何有用的用例吗?

Here's a piece of code to illustrate the concept you are trying to understand. 这是一段代码,用以说明您试图理解的概念。

var callback = function(err, result){
    if(err){
      console.log(err);
      return err;
    }
    callAnotherFunction(result);
};

function getRandom(cb){
   //do something
   var result = Math.random();
   if(result > 0.5){
     return cb(result);
   }
   return cb(null, result);
}
getRandom(callback);

The function getRandom() will return an error if the random number is less than 0.5, a value otherwise. 如果随机数小于0.5,则函数getRandom()将返回错误,否则返回一个值。 The callback then handles the response differently depending on the values passed to the callback function. 然后,回调将根据传递给回调函数的值来不同地处理响应。 As you can see, the return statement inside the callback makes a big difference. 如您所见,回调内部的return语句有很大的不同。 By the way, the above example demonstrates a standard error-first design pattern in NodeJS. 顺便说一下,上面的示例演示了NodeJS中的标准错误优先设计模式

Theoretically, any async function should return something like a handler, which you can use to track progress, or cancel the on-going async actions, example: 从理论上讲,任何异步函数都应返回类似处理程序的内容,可用于跟踪进度或取消正在进行的异步操作,例如:

var timer = setTimeout(..)
clearTimeout(timer)

Because the return value is the only chance that you can get this handler in a sync manner, but it seems people don't quite care about this and invent all kinds of stuff to solve async cancellation problem, please read more about this on my note on action-js . 因为返回值是您以同步方式获得此处理程序的唯一机会,但是似乎人们不太关心此问题,因此发明了各种方法来解决异步取消问题,请在我的笔记中阅读有关此内容的更多信息。在action-js上

In a perfect world, we should get a handler for every async function: 在理想的情况下,我们应该为每个异步函数获取一个处理程序:

handler = fs.readFile('...', function(err, data){...});
xhr = ajax('...', function(err, data){...});
...

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

相关问题 在$ watch回调中使用$ scope是否有意义? - Does it make sense to use $scope into a a $watch callback? Aarray.map 返回值没有意义 - Aarray.map return value does not makes sense 有没有办法让 setTimeout() 返回一个带有异步回调的值 - Is there a way to make setTimeout() return a value with async callback 在此 switch 语句中执行 `return '';` 或 `return null;` 是否更有意义? - Does it make more sense to do `return '';` or `return null;` from within this switch statement? jQuery传递用于回调的函数,但要使用返回值 - jQuery passing function for callback but make use of return value 在下一次对生成器迭代器的调用中包含一个值是否有意义? - Does it ever make sense to include a value in the first next call to generator iterator? 仅在使用了var语句的块内使用它分配值,才有意义吗? - Does it make sense to assign a value to a variable declared with var statement only inside those blocks where it is used? .then()回调的返回值是否以任何方式影响原始的Promise? - Does the return value of .then() callback affect original promise in any way? 做minify和uglify都有意义吗? - Does it make sense to do both minify and uglify? 缓存event.target是否有意义? - Does it make sense to cache event.target?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM