반환 값을 jquery 얻을 콜백 함수
-
26-12-2019 - |
문제
그것은 나에게 매우 유용 할 수 있으면 해결하는 데 도움이 이 기능:
textParseQuery = (txtSnippet) ->
queryUrl = "http://localhost:8083/txtParse/#{txtSnippet}"
console.log queryUrl
callback = (response) =>
parsed = $.parseJSON response
companies = parsed.map (obj) -> new Company(obj.name, obj.addr)
companies
res = $.get queryUrl, {}, callback
console.log res
하고 싶을 가져올에서 결과 콜백을록 textParseQuery
기능 값을 반환합니다.
해결책 2
나는이 발견 IcedCoffeeScript 돕는 간소화 비동기적으로 제어 흐름 await
고 defer
.이것은 무엇을 달성했다.코드 구조가 어떻게 내가 그것을 그리기
# Search for 'keyword' on twitter, then callback 'cb'
# with the results found.
search = (keyword, cb) ->
host = "http://search.twitter.com/"
url = "#{host}/search.json?q=#{keyword}&callback=?"
await $.getJSON url, defer json
cb json.results
다른 팁
의 콜백은 그의 비동기식의 응답은 콜백에서,그래서 당신은 처리해야의 나머지 부분에서 실행은 콜백(예를 들어, console.log res
가 실행하기 전에 당신의 콜백을 호출하기 때문에 일부의 동기의 실행 ajax call).
textParseQuery = (txtSnippet) ->
queryUrl = "http://localhost:8083/txtParse/#{txtSnippet}"
callback = (response) ->
parsed = $.parseJSON response
companies = parsed.map (obj) -> new Company(obj.name, obj.addr)
# proceed from here
console.log companies
$.get queryUrl, {}, callback
추가적인 참고:지방 화살은 불필요한 여기에,그것은 다시 바인딩은 무엇 this
을 참조하지만,당신이지 참조 this
에서 모든 콜백입니다.만약 당신이 학습 커피,대부분의 편집자가 있는 플러그인 모듈을 신속하게 컴파일한 커피 JS,그래서 사용하는 것을 볼이 주어진 커피문 컴파일하에 JS(예를 들어 살펴보고,diff 사용 ->
고 =>
를 컴파일할 때는 커피)
제휴하지 않습니다 StackOverflow