
因为项目用到 puppeteer 库要写一点 Node.js 代码
大概是 endpoint 调用 logic 的 do
do 里需要用到 aboutPuppeteerMethod 的返回值
但是没等到 aboutPuppeteerMethod 执行完返回 sth 已经先执行了 next
我理解的 async 类似于 golang 的 go 关键字 但是似乎没有找到 channel 配套
想达到在 aboutPuppeteerMethod 增加一个 channel 阻塞等待返回值继续执行后面代码的效果
不知道该怎么写 原来没写过 js 轻喷
function Logic() {} Logic.prototype.do = function (param) { var sth = aboutPuppeteerMethod() var ok = next(sth, param) return ok } async function aboutPuppeteerMethod() { .... await page.goto() return sth } 1 wildnode Apr 24, 2020 Logic.prototype.do = async function (param) { var sth = await aboutPuppeteerMethod() var ok = next(sth, param) return ok } |
用 await 来阻塞等待 async |