Retrying With try/catch Around await

Your port’s board gives up on Hollins Green after half a second, but it also marks Quarry Hill as having no answer. Its new kiosk fails only its first request of the morning, so a second ask would get its holds onto the board. Owen’s Python script retries with try and except around the await, and the port does the same.

Retry Where You Await

A rejected promise throws its error at the await that waits for it, as an exception surfaces at Python’s await. So a try/catch around the await catches it. Here’s Owen’s check_with_retry() next to its port:

Python JavaScript
async def check_with_retry(branch): async function checkWithRetry(branch) {
try: try {
return await check_kiosk(branch) return await checkKiosk(branch);
except ConnectionError as err: } catch (err) {
print(f"{err}, asking again") console.log(`${err.message}, asking again`);
await asyncio.sleep(RETRY_PAUSE) await sleep(RETRY_PAUSE);
return await check_kiosk(branch) return checkKiosk(branch);

RETRY_PAUSE is 200 milliseconds where Python’s is 0.2 seconds, and err.message holds the text of the Error that the stand-in throws. A finally block works as it does in Python.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.