Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array-asyncify

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-asyncify

Async array functions

  • 0.0.4
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

NOTE Not work under node 8.x

Issue

We cannot use async function graceful in array methods such as filter,map,reduce and so on.

So the following code do not work

let a=[1,2,3]
a.map(async (e)=>{
  //some async operations
  //await ...
  return e+1
})
.filter(async (e)=>{
  //some async operations
  return e%2===0
})

Resolution

const { asyncify } = require("array-asyncify")
let delay = (t) => new Promise(res => setTimeout(res), t)
console.log("Example:");
(async () => {

  let a = asyncify([1, 2, 3])
  console.log("ret:")
  let ret = await a
    .map(async (e) => {
      await delay(100);
      return e + 1
    })
    .map(async (e) => {
      await delay(100)
      return e * 2
    })
  console.log(ret)

  try {
    let b = asyncify([1, Promise.resolve(2), 3])
    console.log("sum:", await b.reduce(async (p, n) => {
      console.log("p,n", p, n)
      return p + n
    }))
  } catch (e) {
    console.log(e)
  }


})()

TODO

asyncify other method.

Keywords

FAQs

Package last updated on 14 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc