vue-cli生成的webpack配置中一段代码解惑:dev-server.js有段关于

dev-server.js中有段关于promise的代码如下:


var _resolve

var readyPromise = new Promise(resolve => {

  _resolve = resolve

})


console.log('> Starting dev server...')

devMiddleware.waitUntilValid(() => {

  console.log('> Listening at ' + uri + '\n')

  // when env is testing, don't need open it

  if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {

    opn(uri)

  }

  _resolve()

})


var server = app.listen(port)


module.exports = {

  ready: readyPromise,

  close: () => {

    server.close()

  }

}

为什么要加这个啊?

RISEBY
浏览 502回答 1
1回答

开满天机

加上完整的上下文:var _resolvevar readyPromise = new Promise(resolve => {  _resolve = resolve})console.log('> Starting dev server...')devMiddleware.waitUntilValid(() => {  console.log('> Listening at ' + uri + '\n')  // when env is testing, don't need open it  if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {    opn(uri)  }  _resolve()})var server = app.listen(port)module.exports = {  ready: readyPromise,  close: () => {    server.close()  }}源码里面是把 resolve 方法写在外面了,其实和写在里面是一样的:var readyPromise = new Promise(resolve => {  console.log('> Starting dev server...')  devMiddleware.waitUntilValid(() => {    console.log('> Listening at ' + uri + '\n')    // when env is testing, don't need open it    if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {      opn(uri)    }    resolve()  })})这样是不是就好理解一些了呢?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript