• thenjs的应用


     1 <!doctype html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8 <script src="then.js"></script>
     9 <script>
    10 //    <!-- thenjs第一处,parallel的用法-->
    11     Thenjs.parallel([
    12         function(cb){
    13             var a = [1,1,1,1];
    14             cb(null,a);
    15         },
    16         function(cb){
    17             var b = [2,2,2,2];
    18             cb(null,b);
    19         },
    20         function(cb){
    21             var c = [3,3,3,3];
    22             cb(null,c);
    23         },
    24         function(cb){
    25             var cc = [8,8,8,8,8,8];
    26             var xx = [];
    27             Thenjs.each(cc,function(ecb,p){
    28                 xx.push(p*2);
    29                 ecb(null,null);
    30             }).then(function(err,ps){
    31                 cb(null,xx)
    32             });
    33         }
    34     ]).then(function(cb,result){
    35         console.log(result[0]);
    36         console.log(result[1]);
    37         console.log(result[2]);
    38         console.log(result[3]);
    39     });
    40 //模仿查询回调函数
    41 function task(arg,callback){
    42     setTimeout(callback(null,arg),1);
    43 }
    44 
    45 
    46 //    <!-- thenjs第二处,eachSeries的用法-->
    47 Thenjs.eachSeries([0, 1, 2], function (cont, value) {
    48     task(value * 2, cont);
    49 }).then(function (cont, result) {
    50     console.log(result);
    51 });
    52 
    53 
    54 
    55 //    <!-- thenjs第三处,each的用法-->
    56 Thenjs.each([5, 5, 5], function(ecb,p){
    57 //    task(p* 2, ecb);
    58     ecb(null,p*2)
    59 }).then(function (ecb, result) {
    60     console.log(result);
    61 });
    62 //后期继续收集
    63 </script>
    64 </body>
    65 </html>

     用的比较频繁的就是parallel的用法

    坚持下去就能成功
  • 相关阅读:
    C# 不用添加WebService引用,调用WebService方法
    贪心 & 动态规划
    trie树 讲解 (转载)
    poj 2151 Check the difficulty of problems (检查问题的难度)
    poj 2513 Colored Sticks 彩色棒
    poj1442 Black Box 栈和优先队列
    啦啦啦
    poj 1265 Area(pick定理)
    poj 2418 Hardwood Species (trie树)
    poj 1836 Alignment 排队
  • 原文地址:https://www.cnblogs.com/suoking/p/5053657.html
Copyright © 2020-2023  润新知