"Nodejs development of encrypted currency" 11:1 map skilled use async components to solve the callback big pit __JS

Source: Internet
Author: User
about

"Nodejs development of encrypted currency", is a detailed development of a cryptographic currency products, involving the use of NODEJS development products in all aspects, from the front-end to the background, from the server to the client, from the PC to mobile, encryption, decryption and other links. The code is completely open source, the article is free to share. Related resources See http://ebookchain.org preface

Said earlier, in the Nodejs World "Everything is callback", learning to use Nodejs, the most impossible to avoid is "callback" (with "back" more intuitive). Can not avoid, naturally to actively face, so open source community there are many code flow control solutions. For example: Bluebird,q, and the async to be illustrated here.

This basic technology, community documentation is extremely rich, obviously we do not need to repeat predecessors, so here to take the form of graphics and classification, from the operational aspects. In addition, the careful friend will also from the front source analysis discovers, billion books massively uses the AYSNC, grasps it comprehensively, for understanding and the coding, the effort.

This is the ranking of async on https://npmjs.org, except Lodash, that's it. Also, Bluebird and Q are all in the top 10, and the basic explanation is that using Process Control components is standard for NODEJS processing callbacks. Concept Definition

Official Introduction:

Async is a utility module which provides straight-forward, powerful to functions for working with asynchronous JavaScript.

Simple translation: Async is a practical module that provides simple, direct and powerful functions for processing asynchronous JS. Process Category

Just for good distinction, good memory, a simple summary, the whole process should be the following three cases (not official description): 1. Basic Process

From the point of view of the execution sequence of the program (multiple functions), including sequential execution, parallel execution, mixed execution, and other basic processes. If the functions are dependent on each other, whether or not to limit the number of function execution, and so on, can continue to evolve a variety of. 2. Cycle Flow

The cyclic operation based on a certain condition provides many functions according to the usage of the condition. 3. Collection Process

It's all about the processing of one or a set of data. In reality, a large number of the same or similar data, such as: a large number of files, addresses, centralized processing. Obviously, our first reaction is to loop through the process, but Async provides a tool method (util) for asynchronous operations on the set, such as foreach, which is called the "assemble process".

The concept of using a process can clearly tell us that when using Async.foreach (), there is a difference between an asynchronous operation and a simple ForEach method invocation. Usage Categories

Async provides more than 70 useful functions, all of which follow a convention: The asynchronous function you define (as a task), the last argument must be the callback function, the first argument of the callback function must be error, and the callback function is called once.

These functions are roughly divided into 3 categories, corresponding to the above 3 categories: 1. Basic type (one multitasking)

This corresponds to the basic process and Evolution process section, to a transaction, the operation of multiple tasks, called the form of

Async.funname (Tasks, callback (error, results))

Here's the Funname, including: series, parallel (Parallellimit), Waterfall, Auto (autoinject) and so on. The tasks here can be array, either JSON or just one of them.

If all functions execute successfully, the results in callback will also correspond to the form of tasks, converting the result of the function already executed into array or JSON form; if there is a function error in the tasks, the subsequent execution is terminated, and the callback is called. Errors are the error messages for this function, results or contain only results that have been executed, or result placeholders that contain not executed functions, or nothing that includes direct ignoring. As for results specific, a log statement, you can naturally know, do not have to check the document. 2. Cycle type (multiple single task)

Depending on the criteria, you can use the following form to invoke the

Async.funname (Test, FN, callback)

or

async.dofunname (FN, test, callback)

Here are the funname, including: Whilst (Dowhilst), until (Dountil), during (doduring), Retry (retryable), Times (Timesseries, Timeslimit), Forever (the condition of the function test is not used, the condition is already implied).

The conditional value here can be an expression function, or a specific number of times. The order of the FN and test is very simple, as long as the English meaning to understand can be, for example: Async.dowhilst (), is necessarily first do, after whilst, so the parameters are naturally async.dowhilst (FN, test, callback) 3. Set type (multiple single task)

This official is not used as a process statement, as a set operation method provided, is my personal understanding and fabrication. I think it's better to accept and understand it as a process control. So, to mimic the loop-calling method above, just change the condition test to a collection, and the collection uses array arrays.

Async.funname (arr, iteratee, [callback])

Here's the funname, including:

Each, eachseries, Eachlimit
Foreachof, foreachofseries, Foreachoflimit
map, mapseries, Maplimit
filter, Filterseries, Filterlimit
reject, rejectseries, rejectlimit
Reduce, reduceright
detect, Detectseries, Detectlimit
sortby
Some, somelimit, someseries every, Everylimit,
everyseries concat
, concatseries

Of course, Async also explicitly provides several other process controls, such as: Compose,seq,applyeach (applyeachseries), queue (Priorityqueue), cargo,iterator,race, etc.

In addition, there are several util (tool) functions, such as: apply,nexttick,memoize,unmemoize,ensureasync,constant,asyncify,wrapsync,log,dir,noconflict , timeout, etc., these can greatly facilitate code writing, improve code performance. Brain Map

These explanations and methods, we all put in a brain map, combined with the scene, netizens, can quickly find the right approach.

Source code Interpretation

Look inside the Ebookcoin, async use it. Because in the "entry Program App.js Interpretation", already mentioned, here is not said, later encountered again explained.

Fun Practice

Async official documents provide a number of examples, simple and intuitive. Here, instead of giving examples, we present an interesting question for thinking:

question : AYSNC can be used for recursive calls, such as: A crawler program, traversing a folder all the file information functions, and so on. Why. Summary

This is another cliché of technology sharing, but after writing, the process management of asynchronous operations is clearer. AYSNC is good, but it's not everything, and it's powerless for the code that repeats itself, because the callback for the task is limited to one time. Follow-up, there will be a chance to continue to study it further. links

This series of articles is updated immediately, please follow the links below to get the latest content

Source Text Address: Https://github.com/imfly/bitcoin-on-nodejs

ebook reading: http://bitcoin-on-nodejs.ebookchain.org reference

Async official Documents

Node.js The latest technology stack promise article

JavaScript Promise Mini Book (Chinese version)

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.