异步/等待永远卡住

时间:2018-07-01 18:05:23

标签: javascript asynchronous

编写一个使用async / await但被卡在var r1 = await fetch(url).then((r) => r.text())行似乎永远无法处理的地方的webapp。我在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的调用行,得到了以下错误

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)

1 个答案:

答案 0 :(得分:3)

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

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