gpt4 book ai didi

javascript - 异步/等待永远卡住

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:53:58 25 4
gpt4 key购买 nike

编写一个使用 async/await 的 web 应用程序,但卡在 var r1 = await fetch(url).then((r) => r.text()) 行似乎正在处理的位置永远。我在端口 80 上监听的 Web 服务器甚至没有收到请求。

const fetch = require ('fetch-node')
const express = require('express');
const app = express();

var savedResolve;

app.listen(8079, '127.0.0.1', function() {
console.log('listening on 8079')
})
app.get('/*', async function (req, res) {
console.log(req.path)
res.setHeader('Content-Type', 'text/html');
await task()
res.send('Done')
})


async function task() {
console.log("starting..")
var url = "http://localhost/prod/te.php";
var r1 = await fetch(url).then((r) => r.text())
console.log(r1)
return "done"
}

有什么想法吗?提前致谢!

更新1

感谢@deryck 的建议,在 fetch 调用行附近添加 try 和 catch,得到以下错误

TypeError: Cannot read property 'render' of undefined
at module.exports (/Users/jsmith/learn/node/node_modules/hooks-node/hooks-node.js:8:11)
at module.exports (/Users/jsmith/learn/node/node_modules/fetch-node/fetch-node.js:17:1)
at task (/Users/jsmith/learn/node/te4b.js:22:18)
at /Users/jsmith/learn/node/te4b.js:13:8
at Layer.handle [as handle_request] (/Users/jsmith/learn/node/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/jsmith/learn/node/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/jsmith/learn/node/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/jsmith/learn/node/node_modules/express/lib/router/layer.js:95:5)
at /Users/jsmith/learn/node/node_modules/express/lib/router/index.js:281:22
at param (/Users/jsmith/learn/node/node_modules/express/lib/router/index.js:354:14)

最佳答案

node-fetchfetch-node是两个不同的库。

根据显示的代码,node-fetch 是应该使用的库。

关于javascript - 异步/等待永远卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51125759/

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