【发布时间】:2020-10-23 00:07:52
【问题描述】:
我对 NodeJs 中的异步机制有点困惑。如您所见,我在下面有一个路由器类。我有两个端点,当我有一个对端点'/a'的请求时,响应需要大约 10 秒。在这 10 秒内,尽管我使用了异步代码,但我无法向端点“/b”发出请求。
const express = require('express');
const fetch = require("node-fetch");
const router = express.Router();
router.route('/a')
.get(function (req, res) {
fetch(url)
.then(response => response.json())
.then(data => {
**SOME CODE BLOCKS, IT TAKES 10 Seconds.**
res.send(data)
}
);
});
router.route('/b')
.get(function (req, res) {
res.send("Hello");
});
module.exports = router;
请有人解释一下这种行为。
谢谢。
【问题讨论】:
标签: javascript node.js express asynchronous