BLUEBIRD-API Introduction and Demo

Source: Internet
Author: User

varPromise = require ("Bluebird");varFS = require ("FS");//Method of PromisevarReadfileasync =promise.promisify (fs.readfile);//. Spread ([function Fulfilledhandler] [, function Rejectedhandler]), Promise//usage: Splitting the result set//ExamplePromise.delay (0). Then (function () {    return[Readfileasync (' 1.txt ', ' utf-8 '), Readfileasync (' 2.txt ', ' utf-8 '), Readfileasync (' 3.txt ', ' utf-8 ')    ];}). Spread (function(File1, File2, File3) {console.log (file1);    Console.log (file2); Console.log (FILE3);})//. Finally (Function handler), Promise//similar to try: Catch.. Finally in the role of finally//ExamplePromise.delay (0). Then (function () {    return[Readfileasync (' 1.txt ', ' utf-8 '), Readfileasync (' 2.txt ', ' utf-8 '), Readfileasync (' 4.txt ', ' utf-8 ')    ];}). Spread (function(File1, File2, File3) {console.log (file1);    Console.log (file2); Console.log (FILE3);}).Catch(function(e) {console.log (e);}).finally(function () {    //The final error is still executed in the end;Console.log (4)})//Promise.join (promise| Thenable|value promises, Function handler)-Promise//join a few promise functions and use a bit like spread//ExamplevarJoin =Promise.join;join (Readfileasync (' 1.txt ', ' utf-8 '), Readfileasync (' 2.txt ', ' utf-8 '), Readfileasync (' 3.txt ', ' utf-8 '),function(File1, File2, file3) {returnparseint (file1) + parseint (file2) +parseint (FILE3); }). Then (function(content) {Console.log ("Sum result:" +content); })//Synchronous inspection Synchronous Detection//ExamplevarRf1 = Readfileasync (' 1.txt ', ' utf-8 ');varRf2 = Readfileasync (' 2.txt ', ' utf-8 ');varRf3 = Readfileasync (' 3.txt ', ' utf-8 ');varJoin =Promise.join;join (Rf1, Rf2, Rf3,function(File1, File2, file3) {returnparseint (file1) + parseint (file2) +parseint (FILE3);}). Then (function(content) {Console.log ("Sum result:" +content);}).finally(function () {    //. Isfulfilled (), Boolean    //detection is completeConsole.log ("Success:" +rf1.isfulfilled ()); //. isrejected (), Boolean    //Detect if FailureConsole.log ("Fail:" +rf1.isrejected ()); //. Ispending (), Boolean    //detect if in progressConsole.log ("Pending:" +rf1.isrejected ()); //. Value (), dynamic    //The result of success, the general use of the first to determine whether to complete    if(rf1.isfulfilled ()) {Console.log (Rf1.value ()); }    //. Reason (), dynamic    //the reason for failure, the same as the use of the first to determine whether failure    if(rf1.isrejected ()) {Console.log (Rf1.reason ()); }})//. All (), Promise//The parameter is an array, and the inside is promise, and all the successful returns are also the arrays//ExamplevarRFALL1 = Readfileasync (' 1.txt ', ' utf-8 ');varRfAll2 = Readfileasync (' 2.txt ', ' utf-8 ');varRfAll3 = Readfileasync (' 3.txt ', ' utf-8 ');varFiles =[RfAll1, RfAll2, RfAll3]; Promise.all (Files). Then (function(s) {Console.log ("All:" +s)});//. Props (), Promise//similar to. All (), but the parameter is object, all successful return values are also object//ExamplePromise.props ({rfprop1:readfileasync (' 1.txt ', ' utf-8 '), Rfprop2:readfileasync (' 2.txt ', ' utf-8 '), Rfprop3:readfileasync (' 3.txt ', ' utf-8 ')}). Then (function(content) {Console.log (json.stringify (content));})//. Settle (), Promise//basically equivalent to. all ();//ExamplevarRfsettle1 = Readfileasync (' 1.txt ', ' utf-8 ');varRfsettle2 = Readfileasync (' 2.txt ', ' utf-8 ');varRfsettle3 = Readfileasync (' 3.txt ', ' utf-8 ');varFiles =[Rfsettle1, Rfsettle2, Rfsettle3]; Promise.all (Files). Then (function(s) {Console.log ("Settle:" +s)});//. Some (int count), Promise//The first parameter is an array, the second is a number, and the return value is the first to return a successful value//Examplevarrfsome1 = Readfileasync (' 1.txt ', ' utf-8 ');varrfsome2 = Readfileasync (' 2.txt ', ' utf-8 ');varRfsome3 = Readfileasync (' 3.txt ', ' utf-8 ');varFiles =[Rfsome1, Rfsome2, Rfsome3]; Promise.some (Files,2). Spread (function(First,second) {Console.log ("Some:" +First ); Console.log ("Some:" +second);})//. Map (Function mapper [, Object options]), Promise//The parameter is an array and does not need to be promise, as long as the function inside the map is promise. A sort of map method similar to array//ExamplevarFiles = [' 1.txt ', ' 2.txt ', ' 3.txt ']; Promise.map (Files,function(file) {returnReadfileasync (file, ' Utf-8 ');}). Then (function(content) {Console.log ("Map:" +content);})//. Reduce (Function reducer [, Dynamic InitialValue]), Promise//The concept is somewhat like an array of reduce methods. Total is the returned assembly value, and filename is item,0 as the initial value//ExamplePromise.reduce (["1.txt", "2.txt", "3.txt"],function(Total, fileName) {returnReadfileasync (FileName, "UTF8"). Then (function(contents) {returnTotal + parseint (contents, 10); });}, 0). Then (function(total) {Console.log ("Reduce:" +Total )});

Project Address: Https://github.com/shadow88sky/bluebird-api/tree/master

BLUEBIRD-API Introduction and Demo

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.