串联执行函数

发布于 2024-10-10 06:49:46 字数 474 浏览 4 评论 0原文

您能否向我解释一下,串行执行函数是如何工作的?
我浏览了 Connect 的源代码,但我不明白。

我想自己写一个。

app.get(
  '/',
  function(req, res, next) {
    // Set variable        
    req.var = 'Happy new year!';
    // Go to next function
    next();
  },
  function(req, res, next) {
    // Returns 'Happy new year!' 
    console.log(req.var); // <- HOW IS THIS POSSIBLE?
    // (...)
  }      
);

Could you please explain to me, how does executing functions in series works?
I browsed throught Connect's source code, but I don't get it.

I would like to write it by myself.

app.get(
  '/',
  function(req, res, next) {
    // Set variable        
    req.var = 'Happy new year!';
    // Go to next function
    next();
  },
  function(req, res, next) {
    // Returns 'Happy new year!' 
    console.log(req.var); // <- HOW IS THIS POSSIBLE?
    // (...)
  }      
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-10-17 06:49:46

看起来好像您提供的第一个函数参数首先被 get() 函数调用。

调用时,会向调用提供 3 个参数。在调用内部,req 参数必须是可以为其分配属性的对象。您已分配 var 属性,并为其指定值 'Happy new Year!'

您传递的下一个函数参数是通过调用 next() 参数来调用的,并且再次向该调用提供 3 个参数。第一个参数显然与分配给 var 属性的第一次调用的对象相同。

因为它(显然)是同一个对象,所以您分配的属性仍然存在。

这是一个简单的示例: http://jsfiddle.net/dWfRv/1/ (open您的控制台)

// The main get() function. It has two function parameters.
function get(fn1, fn2) {
      // create an empty object that is passed as argument to both functions.
    var obj = {};
      // create a function that is passed to the first function, 
      //   which calls the second, passing it the "obj".
    var nxt = function() {
        fn2(obj); //When the first function calls this function, it fires the second.
    };
      // Call the first function, passing the "obj" and "nxt" as arguments.
    fn1(obj, nxt);
}

// Call get(), giving it two functions as parameters
get(
  function(req, next) {
      // the first function sets the req argument (which was the "obj" that was passed).
    req.msg = 'Happy new year';
      // the second function calls the next argument (which was the "nxt" function passed).
    next();
  }, 
  function(req) {
     // The second function was called by the first via "nxt",
     //   and was given the same object as the first function as the first parameter,
     //   so it still has the "msg" that you set on it.
    console.log(req.msg);
  }
);

请注意,这是一个非常简化的示例,函数中的参数较少。也不是因为 var 是保留字,所以我将 var 更改为 msg

It appears as though the first function argument you provide gets called by the get() function first.

When it is called, 3 parameters are provided to the call. Inside the call, the req parameter must be an object to which you can assign properties. You've assigned the var property, and given it a value of 'Happy new year!'.

The next function argument you pass is called via a call to the next() parameter, and 3 parameters are again provided to the call. The first parameter is apparently the same object that was given to the first call where you assigned the var property.

Because it is (apparently) the same object, the property you assigned is still present.

Here's a simple example: http://jsfiddle.net/dWfRv/1/ (open your console)

// The main get() function. It has two function parameters.
function get(fn1, fn2) {
      // create an empty object that is passed as argument to both functions.
    var obj = {};
      // create a function that is passed to the first function, 
      //   which calls the second, passing it the "obj".
    var nxt = function() {
        fn2(obj); //When the first function calls this function, it fires the second.
    };
      // Call the first function, passing the "obj" and "nxt" as arguments.
    fn1(obj, nxt);
}

// Call get(), giving it two functions as parameters
get(
  function(req, next) {
      // the first function sets the req argument (which was the "obj" that was passed).
    req.msg = 'Happy new year';
      // the second function calls the next argument (which was the "nxt" function passed).
    next();
  }, 
  function(req) {
     // The second function was called by the first via "nxt",
     //   and was given the same object as the first function as the first parameter,
     //   so it still has the "msg" that you set on it.
    console.log(req.msg);
  }
);

Note that this is a very simplified example, with fewer parameters in the functions. Not also that I changed var to msg since var is a reserved word.

泡沫很甜 2024-10-17 06:49:46

如果需要,请尝试使用异步模块。它使能够串联、并联运行或使用池变得更加容易。

https://github.com/caolan/async

If you want, try using the async module. It makes thing much easier to be able to run in series, parallels or use a pool.

https://github.com/caolan/async

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文