2017-10-27 1 views
-1

NodeJS를 사용하고 async/await로 코드를 래핑하려고 시도하지만 매번 "SyntaxError : Unexpected identifier"오류가 발생합니다. 여기 내 코드 :NodeJS - 예기치 않은 식별자가 있습니다.

async function showOff(phone) { 
    return new Promise((resolve, reject) => { 
     var message = 'Hey friend, I have a new ' + phone.color + ' ' + phone.brand + ' phone'; 
     resolve(message); 
    }); 
}; 

let message = await showOff({ color: "black", brand: "Sony" }); 

무엇이 문제입니까?

+0

될 수 있습니다 어떤 경우에는 익명 aync 기능

(async() => { async function showOff(phone) { return new Promise((resolve, reject) => { var message = 'Hey friend, I have a new ' + phone.color + ' ' + phone.brand + ' phone'; resolve(message); }); }; let message = await showOff({ color: "black", brand: "Sony" }); console.log(message); })(); 

에 모든 코드를 래핑 할 수 너는 사용한다? 비동기 지원은 내가 추측 한 최신 버전에서만 사용할 수 있습니다. http://node.green/#ES2017-features-async-functions – Michael

+0

[ 'Node.js 7.5에서'예상치 못한 식별자를 기다리고 있습니다 '] 가능한 복제본 (https://stackoverflow.com/questions/42225480/await-unexpected-identifier) -on-node-js-7-5) –

답변

1

awaitasync 기능 안에서만 사용할 수 있습니다. 기능하는

function showOff(phone) { 
 
    return new Promise((resolve, reject) => { 
 
    var message = 'Hey friend, I have a new ' + phone.color + ' ' + phone.brand + ' phone'; 
 
    resolve(message); 
 
    }); 
 
}; 
 

 
async function phone() { 
 
    let message = await showOff({ color: "black", brand: "Sony" }); 
 
    console.log(message); 
 
} 
 

 
phone();

async 의미는 응답이 아닌 비동기 동작을 수행하는 기능을 기다리고있다. 문서 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

The await operator is used to wait for a Promise. It can only be used inside an async function.

에서

1

그래서 당신은 단순히 버전 않습니다를 nodejs 무엇 간단한 솔루션

관련 문제