3 ms·
If I understand correctly, everything after "await ..., defer" block will be executed only after the asynchronous operations from the await block are completed.
by jarek-foksa 15y ago
If I understand correctly, everything after "await ..., defer" block will be executed only after the asynchronous operations from the await block are completed.
Does it mean that it's impossible to return any value from a function that contains await block?
module =
dataPath: "http://search.twitter.com/search.json?q=blah&callback=?"
init: () ->
# Fetch data and do some stuff with it
await
$.getJSON @dataPath, defer @data
# Return yourself (this won't work)
return @
- timruffles 15y agoThere is a way around it. Here, everything after the await is transformed into a callback for the await magic: waity = (forFn) -> await forFn defer r1, r2 console.log "values of defer variables", r1, r2 return "this only returns from the result callback, not waity" syncReturn = waity (cb) -> setTimeout -> cb "r1 val", "r2 val" console.log "waity's synchronous return value:", syncReturn So our output is: waity's synchronous return value: undefined values of defer variables r1 val r2 val To return, you can simply write: waity = (forFn) -> setTimeout -> await ... return "works as normal"