V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
benb
V2EX  ›  JavaScript

要不 promisify 一下原生数组?

  •  
  •   benb · 2021-01-04 17:08:25 +08:00 · 1633 次点击
    这是一个创建于 1180 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这样 map 、filter 、reduce 这些方法也可以传递 async 函数了……

    import Prray from 'prray'
    
    // 1) create
    const urls = Prray.from(['www.google.com', 'npmjs.org'])
    
    
    // 2) async method
    const responses = await urls.mapAsync(fetch)
    
    
    // 3) method chaining with both normal and async methods
    await urls
      .concat(['github.com', 'wikipedia.org'])
      .mapAsync(request)
      .filter(isValidHtml)
      .forEachAsync(saveToDB)
    
    
    // 4) concurrency limit
    const responses = await urls.mapAsync(fetch, { concurrency: 10 })
    

    工具包的文档和路径: https://github.com/Bin-Huang/prray

    2 条回复    2021-01-04 18:24:39 +08:00
    gouflv
        1
    gouflv  
       2021-01-04 17:15:18 +08:00 via iPhone
    看到了 rxjs 的影子
    xiaoming1992
        2
    xiaoming1992  
       2021-01-04 18:24:39 +08:00 via Android
    粗略瞄了一眼,挺有意思的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5280 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:10 · PVG 17:10 · LAX 02:10 · JFK 05:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.