coffeescript - return value from a jquery get callback function -
it useful me if me fix function:
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
i fetch results callback textparsequery
function return value.
the point of callback it's asynchronous, response comes in callback, need handle rest of execution callback (e.g., console.log res
going execute before callback called, since it's part of same synchronous execution of 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 here console.log companies $.get queryurl, {}, callback
additional note: fat arrow unnecessary here, it's used rebind this
refers to, aren't referencing this
@ in callback. if you're learning coffee, editors have plugin/modules compile coffee js, use see given coffee syntax compiles in js (e.g., take @ diff between using ->
, =>
when compile coffee)
Comments
Post a Comment