Skip to content

Instantly share code, notes, and snippets.

@drslump
Created November 10, 2012 22:14
Show Gist options
  • Save drslump/4052730 to your computer and use it in GitHub Desktop.
Save drslump/4052730 to your computer and use it in GitHub Desktop.
async/await demo
# Using Python/Mozilla style coroutine-like generators (yield as
# expression) and Boo's metaprogramming facilities to easily model
# asynchronous APIs (Promise/A based) into sequential code logic.
# Avoiding the need to for nested callbacks and handling error
# conditions with standard try/except blocks.
#
# Change the ENDPOINT variable to an invalid URL to check the
# error handling.
#
import Async
import Browser(alert)
import Api.jQuery as jq
[async] def foo():
global ENDPOINT
status = jq('#status')
status.html('start')
try:
result = jq('#result')
status.html('Fetching remote resource waiting 3 seconds at least')
await data, delay = jq.post(ENDPOINT, {'html': 'Hello World!'}), sleep(3s)
status.html('Remote resource fetched')
result.html(data)
await result.fadeIn(delay).promise()
status.html("Sleeping for $delay milliseconds")
await sleep(delay)
result.fadeOut()
except:
alert('Error downloading')
status.html('end')
foo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment