Node pits tutorial--Common library

Source: Internet
Author: User

As functional programming, Process control and library of functions are essential (it should be).

Below we introduce two commonly used libraries.

Lodash: Complete API See, Https://lodash.com/docs. Here we show just a few simple examples.

Installation

E:\PROJECT\HERON-LESSON\DEMO4>NPM Install Lodash--save

1 varData1 = [1, 2, 3, 4, 5];2 varData2 = [3{' user ': ' Barney ', ' age ': $, ' active ':true},4{' User ': ' Fred ', ' age ': +, ' active ':false},5{' user ': ' Pebbles ', ' age ': 1, ' active ':true}6 ];7 8 var_ = require (' Lodash ');9 Ten //find only returns a single piece of data, no condition returns the first one by default One varR1 =_.find (data1); AConsole.log (R1);//1 -  - varr2 = _.find (data1,function(item) { the    returnItem > 2; - }); -Console.log (R2);//3 -  + varR3 = _.find (Data2,function(item) { -    returnItem.age > 20; + }); AConsole.log (R3);//{User: ' Barney ', Age:36, active:true} at  -  - //filter returns all data that satisfies the condition, no condition returns all - varR4 =_.filter (data1); -Console.log (R4);//, [1, 2, 3, 4, 5] -  in varR5 = _.filter (Data1,function(item) { -    returnItem > 2; to }); +Console.log (R5);//, [3, 4, 5] -  the varR6 = _.filter (data2,function(item) { *     returnItem.age > 20; $ });Panax NotoginsengConsole.log (R6);//-- [{User: ' Barney ', Age:36, active:true}, {User: ' Fred ', Age:40, active:false}]

We demonstrate the use of two functions, the data of the virtual two arrays, the Find function returns an element of the array, and the filter function returns more than one. It is important to note that even if the filter does not return a single piece of data, an empty array is returned [].

About Process Control

Let's take a look at this example, we'll start by simulating two asynchronous functions.

1 varFIRSTFN =function(callback) {2SetTimeout (function () {3         returnCallback (' This is first task. '));4}, 200);5 };6 varSECONDFN =function(callback) {7SetTimeout (function () {8         returnCallback (' This is second task. '));9}, 10);Ten};

Then look at the wording.

1 firstfn (function  (message) {2    console.log (message); 3 }); 4 secondfn (function  (message) {5    console.log (message); 6 });

The result is

This is second task.
This is first task.

Did not get the results in code order. This is due to the difference in asynchronous task latency. The function of the order can be nested in the previous callback to achieve the desired result. Like this

1 firstfn (function  (message) {2    console.log (message); 3     SECONDFN (function  (message) {4        console.log (message); 5     }); 6 });

However, by doing so, too much nesting will make your code all nested parentheses, difficult to read and modify.

So we're going to follow this library to improve the code.

E:\PROJECT\HERON-LESSON\DEMO4>NPM Install Q--save

This is an implementation of the promise standard, and we can write promise-style code.

Each step of this notation returns promise, so our previous FN (err, data)-style function is also changed to promise style.

1 varFIRSTFN =function () {2     vardefer =Q.defer ();3SetTimeout (function () {4Console.log (' This is first task. '))5 defer.resolve ();6}, 200);7     returndefer.promise;8 };9 varSECONDFN =function () {Ten     vardefer =Q.defer (); OneSetTimeout (function () { AConsole.log (' This is second task. ')) - defer.resolve (); -}, 10); the     returndefer.promise; - }; -  -  + varQ = require (' q ')); -  + FIRSTFN () A. Then (function () { at         returnSECONDFN (); -     }) -. Done (function () { -Console.log (' Done ') -});

Get results

This is first task. This is second Task.done

Async Style example, please jump to another functional programming first step--Process Control

Node pits tutorial--Common library

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.