All Projects → YannickDot → Taskorama

YannickDot / Taskorama

Licence: mit
⚙ A Task/Future data type for JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Taskorama

React Organism
Dead simple React state management to bring pure components alive
Stars: ✭ 219 (+143.33%)
Mutual labels:  async, promise, functional-programming
Fluture
🦋 Fantasy Land compliant (monadic) alternative to Promises
Stars: ✭ 2,249 (+2398.89%)
Mutual labels:  async, promise, functional-programming
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (+47.78%)
Mutual labels:  async, promise, functional-programming
Fun Task
Abstraction for managing asynchronous code in JS
Stars: ✭ 363 (+303.33%)
Mutual labels:  async, promise, functional-programming
Node Qiniu Sdk
七牛云SDK,使用 ES2017 async functions 来操作七牛云,接口名称与官方接口对应,轻松上手,文档齐全
Stars: ✭ 44 (-51.11%)
Mutual labels:  async, promise
Breeze
Javascript async flow control manager
Stars: ✭ 38 (-57.78%)
Mutual labels:  async, promise
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (-45.56%)
Mutual labels:  async, promise
Frame Scheduling
Asynchronous non-blocking running many tasks in JavaScript. Demo https://codesandbox.io/s/admiring-ride-jdoq0
Stars: ✭ 64 (-28.89%)
Mutual labels:  tasks, async
Vue Loadable
⏳ Improve your loading state control with pretty simple methods and helpers.
Stars: ✭ 23 (-74.44%)
Mutual labels:  async, promise
Download
Download and extract files
Stars: ✭ 1,064 (+1082.22%)
Mutual labels:  async, promise
Alecrimasynckit
async and await for Swift.
Stars: ✭ 89 (-1.11%)
Mutual labels:  tasks, async
Fritzbox.js
☎️ The leading AVM Fritz!Box API for NodeJS and JavaScript.
Stars: ✭ 36 (-60%)
Mutual labels:  async, promise
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (-62.22%)
Mutual labels:  async, promise
Emacs Async Await
Async/Await for Emacs
Stars: ✭ 47 (-47.78%)
Mutual labels:  async, promise
Nodespider
[DEPRECATED] Simple, flexible, delightful web crawler/spider package
Stars: ✭ 33 (-63.33%)
Mutual labels:  async, promise
Unityasync
Task and Async Utility Package for Unity. Start co-routines from anywhere.
Stars: ✭ 58 (-35.56%)
Mutual labels:  tasks, async
Emittery
Simple and modern async event emitter
Stars: ✭ 1,146 (+1173.33%)
Mutual labels:  async, promise
Flowa
🔥Service level control flow for Node.js
Stars: ✭ 66 (-26.67%)
Mutual labels:  async, promise
Write
Write data to the file system, creating any intermediate directories if they don't already exist. Used by flat-cache and many others!
Stars: ✭ 68 (-24.44%)
Mutual labels:  async, promise
Sake Core
Sake's core interface.
Stars: ✭ 78 (-13.33%)
Mutual labels:  tasks, promise


Taskorama

Taskorama

Taskorama is a Task/Future data type for JavaScript

npm licence



Taskorama is an implementation of the Task data type. It is used to express concurrent, asynchronous and cancellable computations using functional programming constructs.

The semantics is pretty close to the ones provided by Promises but it has many subtle differencies explained in the Rationale section.

Here is an example of how you can use them :

import Task from 'taskorama'

// Let's create a Task
const myTimeoutTask = Task(function (reject, resolve) {

  // complete task succesfully after 3s
  let timer = setTimeout(resolve, 3000)

  // execute `cancel` to stop the timeout
  let cancel = () => clearTimeout(timer)

  return {cancel}
})

// Start `myTimeoutTask`
const myTimeoutExec = myTimeoutTask.fork(
  (rej) => console.log('failure:', rej),
  (res) => console.log('success:', res),
  (err) => console.error('caught error:', err)
)

// Cancel `myTimeoutTask` when you need to !
myTimeoutExec.cancel()

It's like a Promise but pure, deferrable and cancellable 🤗

Install

> npm install --save taskorama

or

> yarn add taskorama

or CDN

https://unpkg.com/taskorama

Usage

The full API docs is available here : Taskorama Docs

Creating a Task

const task = Task((resolve, reject) => {
  // Do your thing ...
  resolve(the_result)

  // An error ?
  reject(the_error)

  // A closure that cancels stuff running in the task
  const cancel = () => console.log('cancelled !')

  // return it inside an object
  return {cancel}
})

Running (forking) and cancelling a Task

// the failure handler
const failureEffect = (err) => {}

// the success handler
const successEffect = (res) => {}

// the error handler (optional)
const errorEffect = (res) => {}

// Let's start the task
const runningTask = task.fork(
  failureEffect,
  successEffect,
  errorEffect
)

// Let's cancel it
runningTask.cancel() // --> 'cancelled !'

Rationale

I created this lib when I tried to implement Tasks in JavaScript in order to understand how do they work.

I like using Promises but they have major design flaws IMHO :

  • They run as soon as the promise is declared : what if I want to defer the computation ?
  • They are async by default : i.e. Promise.resolve(2) always runs on the next tick
  • They are not cancellable (it's unfortunate that we can't cancel window.fetch http request when necessary)

Tasks happen to be really simple and have richer semantics than Promises :

  • Tasks are pure : they do not perform any side-effect as long as they are not executed by calling .fork() .
  • Tasks make a clear separation between definition and execution, while Promises mix the two. @andrestaltz explained it way better than me in this comment and this post

So I decided to replace Promises by Tasks in my code in order to separate pure data processing resulting of an async computation and side-effects.

I started this project after watching this talk about Observables.

The internals of Taskorama are similar to the one used in case explained in this video.

License

Taskorama is released under the MIT license. See LICENSE for details.

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].