{"id":18711957,"url":"https://github.com/dustinlarimer/multitask","last_synced_at":"2025-04-12T12:31:10.133Z","repository":{"id":25506606,"uuid":"28938054","full_name":"dustinlarimer/multitask","owner":"dustinlarimer","description":"Gulp-style control flow for keeping asynchronous tasks in line","archived":false,"fork":false,"pushed_at":"2015-04-24T21:27:35.000Z","size":188,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-04T13:17:56.760Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dustinlarimer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-07T22:51:12.000Z","updated_at":"2018-01-07T10:30:29.000Z","dependencies_parsed_at":"2022-08-06T04:15:14.948Z","dependency_job_id":null,"html_url":"https://github.com/dustinlarimer/multitask","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinlarimer%2Fmultitask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinlarimer%2Fmultitask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinlarimer%2Fmultitask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinlarimer%2Fmultitask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dustinlarimer","download_url":"https://codeload.github.com/dustinlarimer/multitask/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248566475,"owners_count":21125673,"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":[],"created_at":"2024-11-07T12:40:45.246Z","updated_at":"2025-04-12T12:31:09.836Z","avatar_url":"https://github.com/dustinlarimer.png","language":"JavaScript","readme":"# multitask\n\n## How it works\n\n### Create tasks\n\n`.set` defines a task and its optional dependencies. Tasks and task dependencies are wrapped with [Bluebird](https://github.com/petkaantonov/bluebird) promises. Each task will be called with a last argument (explained below) containing a \"done\" function that completes the task and optionally returns a value.\n\n```javascript\nmultitask\n  .set('first task', function(done){\n    // do some asynchronous work and call done()\n    done({ response: \"tada!\" });\n  })\n  .set('next task', ['list', 'of', 'dependent', 'tasks'], function(res, done){\n    // notice this task depends on others ^\n    // insert an argument to get an array of values returned by them ^ ...\n    done({ task_count: res.length });\n  })\n  .set('task three', ['first task', 'next task'], function(done){\n    // ... or omit that extra argument if the results of dependent tasks are not needed\n    // the -last- argument will always be a `done` function\n  });\n```\n\n### Run tasks and handle responses\n\n`.run` returns a [Bluebird](https://github.com/petkaantonov/bluebird) promise, which exposes a `then` method (for handling results) and a `catch` method (for catching errors).\n\n```javascript\nmultitask\n  .run(['list', 'of', 'tasks'])\n  .then(function(res){\n    /* res == array containing results of all tasks */\n  })\n  .catch(function(err){\n    /* err == any errors thrown by tasks */\n  });\n```\n\n### Reset before re-running\n\nIf you're running tasks with a cron or interval, `.reset` will clear out previous results before re-running. This can be useful when hitting an API for time-sensitive data.\n\n```javascript\nmultitask\n  .reset()\n  .run(['first', 'next']);\n```\n\n## Example implementation\n\n```javascript\nvar tasks = require('./');\n\ntasks.set('stage 1', function(done){\n  setTimeout(function(){\n    done(1);\n  }, 1000);\n});\n\ntasks.set('stage 2', ['stage 1'], function(result, done){\n  // receive prior result by providing argument ^\n  setTimeout(function(){\n    done(result * 2);\n  }, 2000);\n});\n\nmtask.set('report', ['stage 2', 'stage 1'], function(result, done){\n  done( result[0] + result[1] );\n});\n\nfunction init(){\n  mtask\n    .reset()\n    .run(['report', undefined, 'also undefined', 'stage 1', 'stage 2'])\n    .then(function(result){\n      console.log('done', result);\n    })\n    .catch(function(err){\n      console.log('err', err);\n    });\n}\n\ninit();\nsetInterval(init, 5000);\n```\n\nOutput\n\n1. 'stage 1' task begins (1000ms pause)\n2. 'stage 2' task begins (2000ms pause)\n3. 'report' task begins\n4. runner exits with arguments `{ '0': [ 2, 1 ], '1': [Function] }`\n5. runner logs 'done' with arguments `{ '0': [ 3, undefined, 'also undefined', 1, 2 ] }`\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdustinlarimer%2Fmultitask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdustinlarimer%2Fmultitask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdustinlarimer%2Fmultitask/lists"}