简体   繁体   English

异步/等待永远卡住

[英]async/await stuck forever

Writing a webapp that uses async/await but got stuck where the line var r1 = await fetch(url).then((r) => r.text()) appears to be handing forever. 编写一个使用async / await但在行var r1 = await fetch(url).then((r) => r.text())似乎永远在处理。 My web server listening on port 80 didn't even receive the request. 我在端口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"
}

Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

Update1 UPDATE1

Thanks to suggestion by @deryck, add try and catch around the line of fetch call, got the following error instead 感谢@deryck的建议,添加try并捕获fetch调用行,得到以下错误

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-fetch and fetch-node are two differnt libraries. node-fetchfetch-node是两个不同的库。

And based on the shown code node-fetch is the library that should be used. 并且基于所显示的代码, node-fetch是应该使用的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM