Node JS asynchronous execution Process Control Module Async Introduction

Source: Internet
Author: User

1.Async Introduction

Sync is a Process Control toolkit that provides direct and powerful asynchronous functionality. JavaScript is designed for node. JS and can also be used directly in the browser.

Async provides approximately 20 functions, including commonly used maps, reduce, filter, ForEach, etc., asynchronous process Control modes include, serial (series), parallel (parallel), waterfall (waterfall), etc.

Project Address: Https://github.com/caolan/async

2. Async Installation
npm install async
3. Introduction to Async functions

Version 0.2.9 Based on Async.

Async mainly implements three parts of the Process Control function:

    • collection: Collections
    • Flow control: Control flow
    • tool class: Utils

1). Collection: Collections

  • Detect: Used to get the first element in the collection that satisfies a condition.
  • SortBy: Sorts the elements in the collection, from small to large, based on the values produced by each element after an asynchronous operation.
  • Some: If there is at least one element in the collection that satisfies the condition, the final callback gets a value of true, otherwise false.
  • Every: If each element in the collection satisfies the condition, the result passed to the final callback is true, otherwise false
  • Concat: Merges the results of multiple asynchronous operations into an array.
  • Each: If you want to perform the same asynchronous operation on all elements in the same collection.

  • Map: Performs an asynchronous operation on each element in the collection to get the result. All results will be aggregated into the final callback. The difference with each is that each only cares about the last value of the operation regardless of the last value, and the map cares about the final result.
  • Filter: Using an asynchronous operation to filter the elements in the collection, it is important to note that iterator's callback has only one parameter and can only receive true or FALSE.
  • Reject:reject is the opposite of filter and discards when the test is true
  • Reduce: You can give us an initial value, use it with each element in the set, and finally get a value. Reduce moves from left to right through elements, and if you want to right-to-left, you can use Reduceright.

2). Process controls: Control flow

    • Dountil:dountil is the opposite of dowhilst, when test is false, it loops, and true when it jumps out. Other features are consistent.
    • Forever: The callback is never executed, regardless of the condition loop execution, if there is no error.
    • Waterfall: Executes a set of functions sequentially. The value generated by each function is passed to the next.

  • Series: serial execution, each function in an array of functions, to execute the next function after each function is completed.
  • Parallel: Executes multiple functions in parallel, each function is executed immediately, without waiting for other functions to execute first. The data in the array passed to the final callback is in the order declared in the tasks, rather than in the order in which the completion is performed.
  • Whilst: equivalent to while, but the asynchronous call in it will be completed before the next loop.
  • Dowhilst: Equivalent to Do...while, Dowhilst Exchange the parameter position of Fn,test, first perform a cycle, then do test judgment.
  • Until:until is the opposite of whilst, when test is false, it loops, and true when it jumps out. Other features are consistent.
  • Compose: Creates a collection of functions that includes a set of asynchronous functions, each of which consumes the return value of the last function. The f (), g (), H () asynchronous functions are combined into the form F (g (H ())), and the return value is obtained by callback.
  • Applyeach: Implements to pass the same parameter to each function in an array, return by callback. If only the first argument is passed, a function object is returned, and I can pass the parameter call.
  • Queue: is a serial message queue that is not executed all at once by limiting the number of worker. When the number of workers is insufficient, the newly added tasks will be queued until a new worker is available.
  • Iterator: Wraps a set of functions into a iterator that, when this iterator is first called, executes the first function in the definition and returns a second function for invocation.
  • Apply: Allows us to pre-bind multiple parameters to a function and generate a new function that can be called directly, simplifying the code.
  • Nexttick: As with Nodejs's Nexttick, the function is finally called.
  • Cargo: A serial message queue, similar to queue, is not executed all at once by limiting the number of workers. The difference is that cargo each time a task is loaded as a task unit, and the new task cell is loaded only after all the execution of the task Unit has completed.
  • Auto: Used to handle the execution of multiple tasks that have dependencies.
  • Times: Runs asynchronously, and a multiple can be specified for several calls, and the results are merged into the array to return
  • Timesseries: Similar to time, the only difference is synchronous execution

3). Tool Class: Utils

  • Memoize: Allows a function to cache its computed results in memory. For the same parameters, calculate only once, the next time directly to get the results before the good.
  • Unmemoize: Allows functions that have been cached to return a function reference that is not cached.
  • Dir: Similar to log, except that it calls the browser's Console.dir () function, which is displayed as a DOM view.
  • Noconflict: If you have previously defined an async variable in the global domain, when you import this async.js, you will first save the previous async variable and then overwrite it. Just for the browser side, no use in Nodejs, this cannot be demonstrated here.
  • LOG: Executes an asynchronous function and records its return value, the log output.



Related Article

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.