{"id":16824747,"url":"https://github.com/rpominov/fun-task","last_synced_at":"2025-04-04T07:08:35.604Z","repository":{"id":57242851,"uuid":"60370221","full_name":"rpominov/fun-task","owner":"rpominov","description":"Abstraction for managing asynchronous code in JS","archived":false,"fork":false,"pushed_at":"2022-05-31T15:15:09.000Z","size":205,"stargazers_count":373,"open_issues_count":14,"forks_count":18,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-29T05:09:58.051Z","etag":null,"topics":["async","fp","functional-programming","future","monad","promise","task"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rpominov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-03T18:44:02.000Z","updated_at":"2024-03-01T09:55:48.000Z","dependencies_parsed_at":"2022-09-15T00:50:40.930Z","dependency_job_id":null,"html_url":"https://github.com/rpominov/fun-task","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpominov%2Ffun-task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpominov%2Ffun-task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpominov%2Ffun-task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpominov%2Ffun-task/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rpominov","download_url":"https://codeload.github.com/rpominov/fun-task/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247135144,"owners_count":20889421,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","fp","functional-programming","future","monad","promise","task"],"created_at":"2024-10-13T11:11:53.740Z","updated_at":"2025-04-04T07:08:35.562Z","avatar_url":"https://github.com/rpominov.png","language":"JavaScript","readme":"# fun-task* [![Build Status](https://travis-ci.org/rpominov/fun-task.svg?branch=master)](https://travis-ci.org/rpominov/fun-task) [![Coverage Status](https://coveralls.io/repos/github/rpominov/fun-task/badge.svg?branch=master)](https://coveralls.io/github/rpominov/fun-task?branch=master)\n\nAn abstraction for managing asynchronous code in JS.\n\n\\* The name is an abbreviation for \"functional task\" (this library is based on many ideas\nfrom Functional Programming). The type that library implements is usually referred to in the documentation as just \"Task\".\n\n\n## Installation\n\n### NPM\n\n```\nnpm install fun-task\n```\n\n```js\n// modern JavaScript\nimport Task from 'fun-task'\n\n// classic JavaScript\nvar Task = require('fun-task')\n```\n\n### CDN\n\n```html\n\u003cscript src=\"https://unpkg.com/fun-task/umd/funTask.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  var Task = window.FunTask\n\u003c/script\u003e\n```\n\n\n## What is a Task?\n\nTask is an abstraction similar to Promises. The key difference is that a\nTask represents a computation while a Promise represents only a result of a computation.\nIf we have a Task we can: start the computation; terminate it before it's finished;\nor wait until it finishes, and get the result. While with a Promise we can only get the result.\nThis difference doesn't make Tasks *better*, they are just different, we can\nfind legitimate use cases for both abstractions. Let's review it again:\n\nIf we have a Task:\n\n- We can start the computation that it represents (e.g. a network request)\n- We can choose not to start the computation and just throw task away\n- We can start it more than once\n- While computation is running, we can notify it that we're not interested in the result any more,\nand as a response computation may choose to terminate itself\n- When computation finishes we get the result\n\nIf we have a Promise:\n\n- Computation is already running (or finished) and we don't have any control of it\n- We can get the result whenever it's ready\n- If two or more consumers have a same Promise they all will get the same result\n\nThe last item is important. This might be an advantage of Promises over Tasks. \nIf two consumers have a same Task, each of them have to spawn\ntheir own instance of the computation in order to get the result,\nand they may even get different results.\n\n\n## What is a computation?\n\n```js\nfunction computation(onSuccess, onFailure) {\n  // ...\n  return () =\u003e {\n    // ... cancellation logic\n  }\n}\n```\n\nFrom Task API perspective, computation is a function that accepts two callbacks.\nIt should call one of them after completion with the final result.\nAlso a computation may return a function with cancellation logic, or it can return `undefined`\nif particular computation has no cancellation logic.\n\nCreating a Task from a computation is easy, we just call `task = Task.create(computation)`.\nThis is very similar to `new Promise(computation)`, but Task won't execute `computation`\nimmediately, the computation starts only when `task.run()` is called.\n\n\n## Documentation\n\n- [API reference](https://github.com/rpominov/fun-task/blob/master/docs/api-reference.md)\n- [How exceptions catching work in Task](https://github.com/rpominov/fun-task/blob/master/docs/exceptions.md#how-exceptions-work-in-task)\n- [API comparison with Promises](https://github.com/rpominov/fun-task/blob/master/docs/promise-vs-task-api.md)\n\n## [Flow](https://flowtype.org/)\n\nThe NPM package ships with Flow definitions. So you can do something like this if you use Flow:\n\n```js\n// @flow\n\nimport Task from 'fun-task'\n\nfunction incrementTask\u003cF\u003e(task: Task\u003cnumber, F\u003e): Task\u003cnumber, F\u003e {\n  return task.map(x =\u003e x + 1)\n}\n```\n\n## Specifications compatibility\n\n\u003ca href=\"https://github.com/fantasyland/fantasy-land\"\u003e\n  \u003cimg width=\"50\" height=\"50\" src=\"https://raw.githubusercontent.com/fantasyland/fantasy-land/master/logo.png\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/rpominov/static-land\"\u003e\n  \u003cimg width=\"80\" height=\"50\" src=\"https://raw.githubusercontent.com/rpominov/static-land/master/logo/logo.png\" /\u003e\n\u003c/a\u003e\n\nTask is compatible with [Fantasy Land](https://github.com/fantasyland/fantasy-land) and [Static Land](https://github.com/rpominov/static-land) implementing:\n\n- [Semigroup](https://github.com/fantasyland/fantasy-land#semigroup)\n- [Monoid](https://github.com/fantasyland/fantasy-land#monoid)\n- [Functor](https://github.com/fantasyland/fantasy-land#functor)\n- [Bifunctor](https://github.com/fantasyland/fantasy-land#bifunctor)\n- [Apply](https://github.com/fantasyland/fantasy-land#apply)\n- [Applicative](https://github.com/fantasyland/fantasy-land#applicative)\n- [Chain](https://github.com/fantasyland/fantasy-land#chain)\n- [ChainRec](https://github.com/fantasyland/fantasy-land#chainrec)\n- [Monad](https://github.com/fantasyland/fantasy-land#monad)\n\n## Development\n\n```\nnpm run lobot -- --help\n```\n\nRun [lobot](https://github.com/rpominov/lobot) commands as `npm run lobot -- args...`\n","funding_links":[],"categories":["Libraries"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpominov%2Ffun-task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frpominov%2Ffun-task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpominov%2Ffun-task/lists"}