zoukankan      html  css  js  c++  java
  • 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的用法

    坚持下去就能成功
  • 相关阅读:
    测试覆盖(率)到底有什么用?
    重构遗留程序的一次案例学习(java程序)
    rsync学习
    一次awk脚本的重构
    哪本书是对程序员最有影响、每个程序员都该阅读的书?
    我的阅读编程书籍的好方法
    领域驱动设计和实践
    不要if else的编程
    编码规范的要点
    最牛B的编码套路
  • 原文地址:https://www.cnblogs.com/suoking/p/5053657.html
Copyright © 2011-2022 走看看