2017-11-23 1 views
-1

누군가가 도울 수 있기를 바랍니다. 나는 마법의 거울에서 번역 작업을하고있다. 날씨 모듈의 내부 node_helper.js를 사용합니다. 코드를 보여 드리겠습니다 :나는 약속의 물건으로 미쳤어.

  if (this.config.lang != 'en'){ 
       console.log("in Translate"); 
        translate(alerts.description, {to: this.config.lang}).then((res) => {console.log(res.text); return res;}); 
        translate(alerts.expires, {to: this.config.lang}).then(res => {console.log(res.text)}); 
       translate(alerts.message, {to: this.config.lang}).then(res => {console.log(res.text)}); 
      } 

등등. config.log를 살펴보면 모든 결과를 볼 수 있지만 다음 코드에서 사용할 수 없습니다 .,,, 도움말을 참조하십시오

+0

을, 기능이 구글에서이고 당신이 도움이하고자하는 문제가 무엇 약속 –

+0

을 반환 번역? – jfriend00

+0

내가 말했듯이, 번역의 출력이 올바르게 console.log에 표시되지만 코드 변환 후 3 호출이 내 문제입니다 –

답변

0

Promise.all()을 사용하면 약속을 반환하는 세 가지 작업 수행 한 후 사용 결과 .then() 콜백에서 그 결과를 사용하고 있습니다 : 그런데

 if (this.config.lang != 'en'){ 
      console.log("in Translate"); 
      Promise.all([ 
       translate(alerts.description, {to: this.config.lang}), 
       translate(alerts.expires, {to: this.config.lang}), 
       translate(alerts.message, {to: this.config.lang}) 
      ]).then(function(results) { 
       // results[0], results[1], results[2] contain the three results 
       // use those three values in the code inside this callback 
      }) 
     } 
+0

도움을 주셔서 감사합니다. 내가 ur 코드를 시도하고 내가 console.log ("x =", result [0]); x = [object Object] –

+0

@RobertWeigerstorfer - 시도한 실제 코드를 보여줘야합니다. 내 예제는 당신이 당신의 코멘트에 보여준 것처럼'result [0]'이 아니라'results [0]'을 사용합니다. – jfriend00

+0

감사합니다. 결과가 json 형식이라고 말하는 것을 잊어 버렸습니다. 결과 [0] .text를 입력 할 때 올바른 답을 얻었습니다. –