In layman nodejs--asynchronous programming

Source: Internet
Author: User
Tags sleep function

Function-Type programming

Functions as a class citizen in JavaScript, functions as ordinary objects, can be used as parameters or return values.

higher-order functions : functions as parameters or functions as return values

Asynchronous Programming benefits

Event-driven, non-blocking I/O model

Asynchronous Programming difficulties

(1) exception handling , usually try/catch does not necessarily apply, because callback is not executed in the current tick.

Node in exception handling contracts that the exception is passed back as the first argument of a callback function.

(2) function nesting too deep

(3) code blocking : No sleep function, through setinterval () and settimeout () simulation

(4) multithreaded programming

(5) asynchronous to synchronous

Asynchronous Programming Solutions

(1) Event Publishing, subscription mode

Node itself provides the events module, which is a simple implementation of the Publish/subscribe pattern, and some modules inherit from it, and the event model that differs from the DOM does not exist bubbling.

Addlinstener/on/once Registering events

Removelistener/removealllisteners Delete Events

Emit () trigger Event

The event publish/subscribe pattern itself has no problems with synchronous and asynchronous calls, and in node, the emit () call is mostly associated with the event loop and is triggered asynchronously.

A. If you add more than 10 listeners to an event in the default node, you will get a warning

B. In order to handle the exception, the Eventemittter object will handle the error object specially, if the error event is triggered, Eventemittter will check if there is an error event to listen to the listener, if the addition of the errors will be given to the listener processing, if there is no external catch exception, Will cause the thread to exit.

(2) promise/deferred mode

With the event, the execution process needs to be pre-set, even if the branch needs to be pre-set, which is determined by the Publish/subscribe run system.

The asynchronous call is performed first, the deferred delivery process, and the promise/deferred mode.

In the original API, an event intelligently handles a callback, through which the deferred object can add arbitrary business processing logic to the event.

(3) Process Control Library

(1) tail trigger and next

Tail Trigger: A method that requires a manual call to continue execution of subsequent calls, such as the next method in the Connect module

(2) Async

Https://github.com/caolan/async

Asynchronous Serial execution: Async.series ([fn], FN)

Asynchronous Parallel execution: Async.Parallel ([fn], FN)

Dependency handling for asynchronous calls: Async.waterfall

Automatic Dependency processing: Async.auto

(3) Step

Lighter than async, providing only one interface Step,step (TASK1,TASK2,TASK3)

(4) Wind

Asynchronous concurrency control

asynchronous I/O is very easy to implement concurrent access, but the underlying operating system concurrency is also limited, so concurrency needs to be controlled.

Bagpipe Solutions

A. Controlling concurrency through a queue

B. If the current active asynchronous call is less than the qualified value, remove from the queue

C. If the active call reaches the limit, the call is temporarily stored in the queue

D. At the end of each asynchronous call, remove the new asynchronous call from the queue to execute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.