gpt4 book ai didi

node.js - express.js 不会异步调用相同的路由器回调吗?

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:03 25 4
gpt4 key购买 nike

当调用'/a'时,'/b'可以立即执行。但是如果我调用另一个“/a”,第二个会等待第一个结束。如何使 '/a' 真正被异步调用?

代码:

app.get '/a', (req, res, next) ->
f = ->
res.send 'a'
console.log 'end', new Date()
console.log 'sleep', new Date()
setTimeout f, 10000

app.get '/b', (req, res, next) ->
res.send 'b'

输出:

Express server listening on port 3000 in development mode
sleep Sun Oct 14 2012 12:37:52 GMT+0800 (SGT)
GET /b 200 9ms - 1
end Sun Oct 14 2012 12:38:02 GMT+0800 (SGT)
GET /a 200 10022ms - 1
sleep Sun Oct 14 2012 12:38:02 GMT+0800 (SGT)
end Sun Oct 14 2012 12:38:12 GMT+0800 (SGT)
GET /a 200 10005ms - 1

最佳答案

我明白了原因,这是因为我在同一个浏览器上运行了两个“/a”。我只是尝试在 chromium 中运行一个,在 firefox 中运行另一个,它们是异步处理的。看起来很有趣。

关于node.js - express.js 不会异步调用相同的路由器回调吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12879376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com