最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

我应该使用回调的事件?

IT培训 admin 7浏览 0评论

我应该使用回调的事件?

在浏览快递一些老的文档,我发现这个例子中,使用回调“关闭”事件:

app.get('/page/', function (req, res) {
    res.writeHead(200, { /* . . . */ );
    res.write('\n');
    req.on("close", function () {
        // do something when the connection is closed
    });
});

在这ES10认为好的做法呢?我应该写这样的代码,或者我需要考虑事件的其他方法?

回答如下:

这仍然是标准的做法。正如你所看到的这个例子从4.x (latest) Express.js docs取

app.get('/', function (req, res) {
  res.send('GET request to homepage');
});

有一件事,如果你愿意,你可以做的,就是用arrow function notation来定义您的回调。像这样

app.get('/page/', (req, res) => {
    res.writeHead(200, { /* . . . */ });
    res.write('\n');
    req.on("close", () => {
        // do something when the connection is closed
    });
});

我应该使用回调的事件?

在浏览快递一些老的文档,我发现这个例子中,使用回调“关闭”事件:

app.get('/page/', function (req, res) {
    res.writeHead(200, { /* . . . */ );
    res.write('\n');
    req.on("close", function () {
        // do something when the connection is closed
    });
});

在这ES10认为好的做法呢?我应该写这样的代码,或者我需要考虑事件的其他方法?

回答如下:

这仍然是标准的做法。正如你所看到的这个例子从4.x (latest) Express.js docs取

app.get('/', function (req, res) {
  res.send('GET request to homepage');
});

有一件事,如果你愿意,你可以做的,就是用arrow function notation来定义您的回调。像这样

app.get('/page/', (req, res) => {
    res.writeHead(200, { /* . . . */ });
    res.write('\n');
    req.on("close", () => {
        // do something when the connection is closed
    });
});

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论