• Javascript async异步操作库简介


    异步操作知识

    在js世界中, 异步操作非常流行, nodejs就是特点基于异步非阻塞。

    js语言支持的异步语法包括, Promise  async await generator yield。

    这些语法需要使用者了解非常清楚, 往往很困难。

    下面介绍一个异步操作的超级库,可以实现很多异步操作和流程控制。

    async库

    http://caolan.github.io/async/index.html

    Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via npm install --save async, it can also be used directly in the browser.

    浏览器也可以用!!!

    Async provides around 70 functions that include the usual 'functional' suspects (map, reduce, filter, each…) as well as some common patterns for asynchronous control flow (parallel, series, waterfall…). All these functions assume you follow the Node.js convention of providing a single callback as the last argument of your asynchronous function -- a callback which expects an Error as its first argument -- and calling the callback once.

    包括70个异步函数, 包括 函数类型的 map reduce filter

    以及 异步控制流函数 parallel series

    DEMO

    async.map(['file1','file2','file3'], fs.stat, function(err, results) {
        // results is now an array of stats for each file
    });
    
    async.filter(['file1','file2','file3'], function(filePath, callback) {
      fs.access(filePath, function(err) {
        callback(null, !err)
      });
    }, function(err, results) {
        // results now equals an array of the existing files
    });
    
    async.parallel([
        function(callback) { ... },
        function(callback) { ... }
    ], function(err, results) {
        // optional callback
    });
    
    async.series([
        function(callback) { ... },
        function(callback) { ... }
    ]);
    parallel 并行执行,效率高。 === Promise.all
    series 按照前后顺序执行。
    race 竞态执行。 === Promise.race

  • 相关阅读:
    poj 3928 树状数组
    poj 2528 线段树+离散化
    poj 3468 成段增减
    hdu 2795 线段树(纵向)
    2016年江西理工大学C语言程序设计竞赛(高级组)
    2016年江西理工大学C语言程序设计竞赛(初级组)
    2015年江西理工大学C语言程序设计竞赛(高级组)
    江西理工大学南昌校区2016年新生赛
    2014江西理工大学C语言程序竞赛高级组
    2014江西理工大学C语言程序竞赛初级组
  • 原文地址:https://www.cnblogs.com/lightsong/p/6403832.html
Copyright © 2020-2023  润新知