Coder Social home page Coder Social logo

developit / asyncro Goto Github PK

View Code? Open in Web Editor NEW
488.0 5.0 24.0 1.17 MB

⛵️ Beautiful Array utilities for ESnext async/await ~

Home Page: https://developit.github.io/asyncro

JavaScript 100.00%
async-functions parallel asynchronous accumulator filter iteration promise promises

asyncro's Introduction

asyncro NPM travis-ci

The same map(), reduce() & filter() you know and love, but with async iterator functions!

Do fetch() networking in loops, resolve Promises, anything async goes. Performance-friendly by default.

Here's what it looks like:

Asyncro Example


What's in the Box

Asyncro Example 2


Installation

npm install --save asyncro

Import and Usage Example

import { map } from 'asyncro';

async function example() {
	return await map(
		['foo', 'bar', 'baz'],
		async name => fetch('./'+name)
	)
}

API

reduce

Invoke an async reducer function on each item in the given Array, where the reducer transforms an accumulator value based on each item iterated over. Note: because reduce() is order-sensitive, iteration is sequential.

This is an asynchronous version of Array.prototype.reduce()

Parameters

  • array Array The Array to reduce
  • reducer Function Async function, gets passed (accumulator, value, index, array) and returns a new value for accumulator
  • accumulator [any] Optional initial accumulator value

Examples

await reduce(
	['/foo', '/bar', '/baz'],
	async (accumulator, value) => {
		accumulator[v] = await fetch(value);
		return accumulator;
	},
	{}
);

Returns any final accumulator value

map

Invoke an async transform function on each item in the given Array in parallel, returning the resulting Array of mapped/transformed items.

This is an asynchronous, parallelized version of Array.prototype.map().

Parameters

  • array Array The Array to map over
  • mapper Function Async function, gets passed (value, index, array), returns the new value.

Examples

await map(
	['foo', 'baz'],
	async v => await fetch(v)
)

Returns Array resulting mapped/transformed values.

filter

Invoke an async filter function on each item in the given Array in parallel, returning an Array of values for which the filter function returned a truthy value.

This is an asynchronous, parallelized version of Array.prototype.filter().

Parameters

  • array Array The Array to filter
  • filterer Function Async function. Gets passed (value, index, array), returns true to keep the value in the resulting filtered Array.

Examples

await filter(
	['foo', 'baz'],
	async v => (await fetch(v)).ok
)

Returns Array resulting filtered values

find

Invoke an async function on each item in the given Array in parallel, returning the first element predicate returns truthy for.

This is an asynchronous, parallelized version of Array.prototype.find().

Parameters

  • array Array The Array to find
  • predicate Function Async function. Gets passed (value, index, array), returns true to be the find result.

Examples

await find(
	['foo', 'baz', 'root'],
	async v => (await fetch(v)).name === 'baz'
)

Returns any resulting find value

every

Checks if predicate returns truthy for all elements of collection in parallel.

This is an asynchronous, parallelized version of Array.prototype.every().

Parameters

  • array Array The Array to iterate over.
  • predicate Function Async function. Gets passed (value, index, array), The function invoked per iteration.

Examples

await every(
	[2, 3],
	async v => (await fetch(v)).ok
)

Returns Boolean Returns true if all element passes the predicate check, else false.

some

Checks if predicate returns truthy for any element of collection in parallel.

This is an asynchronous, parallelized version of Array.prototype.some().

Parameters

  • array Array The Array to iterate over.
  • filterer Function Async function. Gets passed (value, index, array), The function invoked per iteration.

Examples

await some(
	['foo', 'baz'],
	async v => (await fetch(v)).ok
)

Returns Boolean Returns true if any element passes the predicate check, else false.

parallel

Invoke all async functions in an Array or Object in parallel, returning the result.

Parameters

Examples

await parallel([
	async () => await fetch('foo'),
	async () => await fetch('baz')
])

Returns (Array | Object) same structure as list input, but with values now resolved.

series

Invoke all async functions in an Array or Object sequentially, returning the result.

Parameters

Examples

await series([
	async () => await fetch('foo'),
	async () => await fetch('baz')
])

Returns (Array | Object) same structure as list input, but with values now resolved.

License

MIT

asyncro's People

Contributors

danielruf avatar developit avatar fimars avatar wkovacs64 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

asyncro's Issues

Next version plan?

Awesome, asyncro is both simple and practical.

Some suggestions:

  • support more Array operators. In fact, most operators are the same pattern like filter.
  • support Object, maybe like lodash.js.

If you have any plan, very happy to make a PR. 🐵

Shouldn't map() run the callback in parallel?

Hi @developit. I had a quick look on your lib, but something seemed rather strange to me.

Why does map() run the callback serially? The nice thing about map is that it provides great opportunities for concurrent processing.

Let's say I do map(['/a', '/b', '/c'], async (item) => await fetch(item)) and every fetch takes 100ms. I would expect the map() to resolve after 100ms, not 300 ms.

Doing filter() and reduce() concurrently might get tricky, but map() should be rather carefree :)

Cheers

Series is executing in parallel

From what I can tell, the series method is actually executing all of the methods when it's called, and then waiting for each one to resolve in series before adding it to the array. I think the expected behavior would be that each method is not called until the previous one has resolved.

series([1,2,3,4].map((n) => {
  return async () => new Promise((resolve) => {
    console.log(n)
    setTimeout(() => resolve(n), 1000)
  })
}))

Expected: Each number is logged out 1 second after the previous, total runtime is ~4 seconds
Actual: All numbers are printed immediately, total runtime is ~1 second

I think one way of solving this would be to remove https://github.com/developit/asyncro/blob/master/src/util.js#L6
And change https://github.com/developit/asyncro/blob/master/src/util.js#L6 to be
acc.push(await v());, but I'm not sure if that's the best way.
** This would break the parallel method, so no go there

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.