【发布时间】:2020-12-22 07:08:21
【问题描述】:
nodejs 有点新,我似乎无法让我的回调函数工作。现在我的函数只想检查 URL 是否存在
const request = require('request')
const URL = 'http://sampleurl.com'
var urlexists = false
async _urlExists(URL){
request(url, function (error, response, body) {
if (!error && response.statusCode.toString()[0] === '2'){
urlexists = true
} else {
throw new Error(`url is not working: ${URL}`)
}
})
}
问题是,我的代码继续执行下一个,这意味着 结果已通过,但几秒钟后,控制台显示错误提示 url 不起作用:${URL }.
基本上我只是打电话
await this._urlExists(URL)
【问题讨论】: