Skip to content

Instantly share code, notes, and snippets.

@danielecr
Created August 29, 2019 19:55
Show Gist options
  • Save danielecr/4fac4a6252c3fd9b20d98629517bf346 to your computer and use it in GitHub Desktop.
Save danielecr/4fac4a6252c3fd9b20d98629517bf346 to your computer and use it in GitHub Desktop.
eventloop based async programming patterns

There are patterns for solving async tasks. Not just promise vs callback. Real problems and real patterns

Problem example 1.

Send messages to a service that give reply async, store data into an array of promises, solve all promises at the end. Additional constraint: the number of message that could be sent to the service is limited, and the only way to test if the limit is reached is to catch an exception on failed sent.

i.e.

try {
  foreach(let request  of requests) {
    send_to_service(request);
  }
} catch(err) {
  // what should I do? wait a timeout and restart?
}

Hints: how many requests are succesfull? then do not repeat success requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment