All Projects → tannerlinsley → Swimmer

tannerlinsley / Swimmer

Licence: mit
🏊 Swimmer - An async task pooling and throttling utility for JS

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Swimmer

P Map
Map over promises concurrently
Stars: ✭ 639 (+579.79%)
Mutual labels:  async, concurrency, promises, await
ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (-78.72%)
Mutual labels:  promises, task, concurrency, await
Vue Concurrency
A library for encapsulating asynchronous operations and managing concurrency for Vue and Composition API.
Stars: ✭ 147 (+56.38%)
Mutual labels:  async, concurrency, task
Hydra
⚡️ Lightweight full-featured Promises, Async & Await Library in Swift
Stars: ✭ 1,954 (+1978.72%)
Mutual labels:  async, promises, await
Breeze
Javascript async flow control manager
Stars: ✭ 38 (-59.57%)
Mutual labels:  async, promises, await
Taskbuilder.fs
F# computation expression builder for System.Threading.Tasks
Stars: ✭ 217 (+130.85%)
Mutual labels:  async, task, await
Modern Async
A modern JavaScript tooling library for asynchronous operations using async/await and promises
Stars: ✭ 31 (-67.02%)
Mutual labels:  async, promises, await
Asyncex
A helper library for async/await.
Stars: ✭ 2,794 (+2872.34%)
Mutual labels:  async, task, await
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (+53.19%)
Mutual labels:  async, concurrency, promises
Promise Pool
Map-like, concurrent promise processing
Stars: ✭ 258 (+174.47%)
Mutual labels:  async, concurrency, promises
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+3920.21%)
Mutual labels:  async, concurrency, promises
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+634.04%)
Mutual labels:  async, promises, await
Kovenant
Kovenant. Promises for Kotlin.
Stars: ✭ 657 (+598.94%)
Mutual labels:  async, concurrency, promises
Asyncawaitbestpractices
Extensions for System.Threading.Tasks.Task and System.Threading.Tasks.ValueTask
Stars: ✭ 693 (+637.23%)
Mutual labels:  async, task, await
Taskmanager
A simple、 light(only two file)、fast 、powerful 、easy to use 、easy to extend 、 Android Library To Manager your AsyncTask/Thread/CallBack Jobqueue ! 一个超级简单,易用,轻量级,快速的异步任务管理器,类似于AsyncTask,但是比AsyncTask更好用,更易控制,从此不再写Thread ! ^_^
Stars: ✭ 25 (-73.4%)
Mutual labels:  async, task
Fennel
A task queue library for Python and Redis
Stars: ✭ 24 (-74.47%)
Mutual labels:  async, task
Alecrimasynckit
async and await for Swift.
Stars: ✭ 89 (-5.32%)
Mutual labels:  async, await
Then
🎬 Tame async code with battle-tested promises
Stars: ✭ 908 (+865.96%)
Mutual labels:  async, task
Koahub Demo
koahub+async/await+mysql
Stars: ✭ 15 (-84.04%)
Mutual labels:  async, await
Csp
Communicating Sequential Processes in JavaScript
Stars: ✭ 33 (-64.89%)
Mutual labels:  async, await

🏊‍ swimmer

David Dependancy Status npm package v npm package dm Join the community on Slack Github Stars Twitter Follow

An async task pooling and throttling utility for javascript.

Features

  • 🚀 3kb and zero dependencies
  • 🔥 ES6 and async/await ready
  • 😌 Simple to use!

Interactive Demo

Installation

$ yarn add swimmer
# or
$ npm i swimmer --save

UMD

https://unpkg.com/swimmer/umd/swimmer.min.js

Inline Pooling

Inline Pooling is great for:

  • Throttling intensive tasks in a serial fashion
  • Usage with async/await and promises.
  • Ensuring that all tasks succeed.
import { poolAll } from 'swimmer'

const urls = [...]

const doIntenseTasks = async () => {
  try {
    const res = await poolAll(
      urls.map(task =>
        () => fetch(url) // Return an array of functions that return a promise
      ),
      10 // Set the concurrency limit
    )
  } catch (err, task) {
    // If an error is encountered, the entire pool stops and the error is thrown
    console.log(`Encountered an error with task: ${task}`)
    throw err
  }

  // If no errors are thrown, you get your results!
  console.log(res) // [result, result, result, result]
}

Custom Pooling

Custom pools are great for:

  • Non serial
  • Reusable pools
  • Handling errors gracefully
  • Task management and retry
  • Variable throttle speed, pausing, resuming of tasks
import { createPool } from 'swimmer'

const urls = [...]
const otherUrls = [...]

// Create a new pool with a throttle speed and some default tasks
const pool = createPool({
  concurrency: 5,
  tasks: urls.map(url => () => fetch(url))
})

// Subscribe to errors
pool.onError((err, task) => {
  console.warn(err)
  console.log(`Encountered an error with task ${task}. Resubmitting to pool!`)
  pool.add(task)
})

// Subscribe to successes
pool.onSuccess((res, task) => {
  console.log(`Task Complete. Result: ${res}`)
})

// Subscribe to settle
pool.onSettled(() => console.log("Pool is empty. All tasks are finished!"))

const doIntenseTasks = async () => {
  // Add more tasks to the pool.
  tasks.forEach(
    url => pool.add(
      () => fetch(url)
    )
  )

  // Increase the concurrency to 10! This can also be done while it's running.
  pool.throttle(10)

  // Pause the pool
  pool.stop()

  // Start the pool again!
  pool.start()

  // Add a single task and WAIT for it's completion/failure
  try {
    const res = await pool.add(() => fetch('http://custom.com/asset.json'))
    console.log('A custom asset!', res)
  } catch (err) {
    console.log('Darn! An error...')
    throw err
  }

  // Then clear the pool. Any running tasks will attempt to finished.
  pool.clear()
}

API

Swimmer exports two functions:

  • poolAll - creates an inline async/await/promise compatible pool
    • Arguments
      • Array[Function => Promise] - An array of functions that return a promise.
      • Int - The concurrency limit for this pool.
    • Returns
      • A Promise that resolves when all tasks are complete, or throws an error if one of them fails.
    • Example:
  • createPool - creates an custom pool
    • Arguments
      • Object{} - An optional configuration object for this pool
        • concurrency: Int (default: 5) - The concurrency limit for this pool.
        • started: Boolean (default: true) - Whether the pool should be started by default or not.
        • tasks: Array[Function => Promise] - An array of functions that return a promise. These tasks will be preloaded into the pool.
    • Returns
      • Object{}
        • add(() => Promise, config{}) - Adds a task to the pool. Optionally pass a config object
          • config.priority - Set this option to true to queue this task in front of all other pending tasks.
          • Returns a promise that resolves/rejects with the eventual response from this task
        • start() - Starts the pool.
        • stop() - Stops the pool.
        • throttle(Int) - Sets a new concurrency rate for the pool.
        • clear() - Clears all pending tasks from the pool.
        • getActive() - Returns all active tasks.
        • getPending() - Returns all pending tasks.
        • getAll() - Returns all tasks.
        • isRunning() - Returns true if the pool is running.
        • isSettled() - Returns true if the pool is settled.
        • onSuccess((result, task) => {}) - Registers an onSuccess callback.
        • onError((error, task) => {}) - Registers an onError callback.
        • onSettled(() => {}) - Registers an onSettled callback.

Tip of the year

Make sure you are passing an array of thunks. A thunk is a function that returns your task, not your task itself. If you pass an array of tasks that have already been fired off then it's too late for Swimmer to manage them :(

Contributing

We are always looking for people to help us grow swimmer's capabilities and examples. If you have an issue, feature request, or pull request, let us know!

License

Swimmer uses the MIT license. For more information on this license, click here.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].