2016-12-27 1 views
7

으로 기다리고 나는이 코드를 가지고 :비동기은 nodejs 7 나는 7.3.0 nodejs 설치 한

let get = async function (url) { 
       ^^^^^^^^ 
    SyntaxError: Unexpected token function 
     at Object.exports.runInThisContext (vm.js:78:16) 
     at Module._compile (module.js:543:28) 
     at Object.Module._extensions..js (module.js:580:10) 
     at Module.load (module.js:488:32) 
     at tryModuleLoad (module.js:447:12) 
     at Function.Module._load (module.js:439:3) 
+0

플래그없이 지원 되셨습니까? 내가 볼 수있는 것부터 (http://kangax.github.io/compat-table/es2016plus/), 당신은 당신의 노드를'--harmony' 또는'--es_staging' 플래그로 생성 할 필요가있는 것처럼 보입니다 . –

답변

9

Node 7.3.0 does not support async/await without a feature flag : 디버그에서

let getContent = function (url) { 
    // return new pending promise 
    return new Promise((resolve, reject) => { 
     // select http or https module, depending on reqested url 
     const lib = url.startsWith('https') ? require('https') : require('http'); 
     const request = lib.get(url, (response) => { 
      // handle http errors 
      if (response.statusCode < 200 || response.statusCode > 299) { 
       reject(new Error('Failed to load page, status code: ' + response.statusCode)); 
      } 
      // temporary data holder 
      const body = []; 
      // on every content chunk, push it to the data array 
      response.on('data', (chunk) => body.push(chunk)); 
      // we are done, resolve promise with those joined chunks 
      response.on('end',() => resolve(body.join(''))); 
     }); 
     // handle connection errors of the request 
     request.on('error', (err) => reject(err)) 
    }) 
}; 

let get = async function (url) { 
    var content = await getContent(url); 
    return content; 
} 

var html = get('https://developer.mozilla.org/it/'); 

내가이 나타납니다. 이 같은 노드를 산란하는 것은 트릭을 수행해야합니다 version 5.5에, V8, 크롬의 자바 스크립트 엔진을 업데이트에서 오는

노드가 이제 공식적으로 async/await by default in version 7.6.0을 지원

node --harmony-async-await app.js 

EDIT.

+0

고맙습니다. 나는 깃발 선택에 대해 몰랐다. – asv

+1

@asv 경고 : 프로덕션에서는 권장하지 않습니다! – Bamieh

+0

또한 노드를 7.6.0 – protspace