Knowledgebase

Asynchronous Patterns in Node Print

  • backenddevelopment, backend, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Working without blocking.

WHAT THE PATTERNS ARE

Callbacks, the original approach Promises The modern syntax reading sequentially

WHAT TO USE

The modern syntax, with promises underneath.

WHY

Nested callbacks become unreadable quickly.

WHAT TO ALWAYS DO

Handle failure.

WHY

An unhandled rejection can terminate the process in current versions.

WHAT TO WRAP

Anything that can fail, which is everything doing input or output.

WHAT TO BE CAREFUL WITH

Awaiting sequentially where operations are independent.

WHY

It makes three parallel operations take three times as long.

WHAT TO DO INSTEAD

Run them concurrently and await all.

WHAT TO BE CAREFUL WITH IN LOOPS

Awaiting inside a loop, serialising work unnecessarily.

WHAT TO SET

Timeouts on every external call.

WHY

A hanging dependency otherwise exhausts your capacity.

WHAT TO IMPLEMENT FOR FLAKY DEPENDENCIES

Retries with increasing delay, and a limit.

WHAT TO AVOID

Retrying operations that are not safe to repeat.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot