게시 한 코드가 동기가 아니므로 올바르게 작동합니다.
이것은 코드가 실제로 무엇을하는지 더 잘 이해할 수있게 해주는 코드의 흐름입니다. 아약스에서 뭔가를 반환 측면에서
// myVar is declared undefined
var myVar;
// you call console.log with undefined
console.log(myVar);
// sometime later .then is called
myModule.myFunction().then(function(data){
// later you set the global variable to the data
myVar = data;
// at this point you can log the value of myVar
});
은 기적을 할 수없는 부르지 만, 당신은 반환의 사슬 다른 .then
후 수 있습니다.
myModule.myFunction().then(function(data){
// do something with the data
return data;
})
.then(function(data){
// do something else with the data
})
당신은 당신이 원하는만큼 then
체인 유지할 수 있으며, then
비동기이기 때문에 당신이 오류 처리에 사용할 수있는 유용한 방법, 또는 병렬로 호출 약속 또는 동 기적으로
합니다. 그런 다음 일부 데이터를 myVar에 할당하지만 그 동안에는 console.loging myVar에 할당하십시오. myVar = data 뒤에 console.log를 .then 함수에 넣음으로써 콘솔 로그가 실행되는지 확인하십시오. ** 데이터가 정의되지 않았는지 확인하십시오. ** –
가능한 [ 응답 비동기 호출에서?] (http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –
@ BenFortune, 당신은 내 질문을 읽었습니까? ? 솔루션이 말하는대로 이미 완료했지만 약속이 반환 된 후 "then"블록 내에서 제어가 차단됩니다. – Krisalay