{"id":19560628,"url":"https://github.com/zoubin/callback-sequence","last_synced_at":"2026-06-13T19:34:09.839Z","repository":{"id":35706639,"uuid":"39984236","full_name":"zoubin/callback-sequence","owner":"zoubin","description":"Make a new callback to run input callbacks in sequence","archived":false,"fork":false,"pushed_at":"2015-12-10T07:13:56.000Z","size":49,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-19T20:02:16.963Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/zoubin.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","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-07-31T03:15:09.000Z","updated_at":"2015-10-05T03:07:59.000Z","dependencies_parsed_at":"2022-08-18T01:21:25.304Z","dependency_job_id":null,"html_url":"https://github.com/zoubin/callback-sequence","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/zoubin/callback-sequence","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubin%2Fcallback-sequence","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubin%2Fcallback-sequence/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubin%2Fcallback-sequence/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubin%2Fcallback-sequence/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zoubin","download_url":"https://codeload.github.com/zoubin/callback-sequence/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubin%2Fcallback-sequence/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34298247,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-13T02:00:06.617Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-11T05:08:18.730Z","updated_at":"2026-06-13T19:34:09.822Z","avatar_url":"https://github.com/zoubin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# callback-sequence\n[![version](https://img.shields.io/npm/v/callback-sequence.svg)](https://www.npmjs.org/package/callback-sequence)\n[![status](https://travis-ci.org/zoubin/callback-sequence.svg?branch=master)](https://travis-ci.org/zoubin/callback-sequence)\n[![coverage](https://img.shields.io/coveralls/zoubin/callback-sequence.svg)](https://coveralls.io/github/zoubin/callback-sequence)\n[![dependencies](https://david-dm.org/zoubin/callback-sequence.svg)](https://david-dm.org/zoubin/callback-sequence)\n[![devDependencies](https://david-dm.org/zoubin/callback-sequence/dev-status.svg)](https://david-dm.org/zoubin/callback-sequence#info=devDependencies)\n\nMake a new callback to run callbacks in sequence or parallel.\n\nCallbacks can be made async like [gulp tasks](https://github.com/gulpjs/gulp/blob/master/docs/API.md#fn).\n\n## Example\n\n```javascript\nvar thunkify = require('callback-sequence')\n\nvar Readable = require('stream').Readable\nvar gulp = require('gulp')\n\ngulp.task('sequence', thunkify(\n  sync, async, promise, stream\n))\n\ngulp.task('parallel', thunkify(\n  [sync, async, promise, stream]\n))\n\ngulp.task('parallel-nested', thunkify(\n  // `async` and `promise` will be run in parallel\n  sync, [async, promise], stream\n))\n\ngulp.task('sequence-nested', thunkify(\n  // `async` and `promise` will be run in sequence\n  [sync, [async, promise], stream]\n))\n\nfunction sync() {\n}\n\nfunction async(cb) {\n  process.nextTick(cb)\n}\n\nfunction promise() {\n  return Promise.resolve()\n}\n\nfunction stream() {\n  var s = Readable()\n  s.push(null)\n  return s\n}\n\n```\n\n## API\n\n```javascript\nvar Runner = require('./lib/runner')\nvar runner = new Runner({ input: false, output: false })\n\nexports = module.exports = runner.thunkify.bind(runner)\nexports.run = exports.sequence = runner.sequence.bind(runner)\nexports.parallel = runner.parallel.bind(runner)\nexports.series = runner.series.bind(runner)\nexports.Runner = Runner\n\n```\n\n### Runner\n`var runner = Runner(opts)`\nCreate a new runner instance.\n\n#### opts\n\n##### input\nSpecify whether to pass the results of the previous callback to the next as arguments.\n\nType: `Boolean`\n\nDefault: `true`\n\n```javascript\nvar Runner = require('callback-sequence').Runner\n\nvar runner = Runner({ input: true })\n\nrunner.thunkify(\n  function (a, b) {\n    // 3\n    return a + b\n  },\n  function (sum, next) {\n    process.nextTick(function () {\n      // 6\n      next(null, sum * 2)\n    })\n  },\n  function (product) {\n    return Promise.resolve().then(function () {\n      // 7\n      return product + 1\n    })\n  }\n)(1, 2)\n.then(function (res) {\n  // [7]\n  console.log(res)\n})\n\n\n```\n\n##### output\nSpecify whether to deliver results.\n\nType: `Boolean`\n\nDefault: `true`\n\nIf `false`, the final results will always be `[]`.\n\n##### run\nSpecify a runner function to run each callback.\n\nType: `Function`, `Object`\n\nDefault: `null`\n\nIf `Function`, it receives a callback followed by a list of arguments,\nand should return a promise to fetch the results (`Array`).\n\nIf `Object`, it is passed to\n[`Runner of run-callback`](https://github.com/zoubin/run-callback#runner--runrunner)\nto create a runner function.\n\n\n#### cb = Runner.prototype.thunkify(...tasks)\nReturn a callback to run the specified tasks in the appearance order.\n\n```javascript\nvar runner = Runner()\n\nrunner.thunkify(\n  function (res) {\n    return res + 1\n  },\n  function (res) {\n    return Promise.resolve()\n      .then(function () {\n        return res + 1\n      })\n  },\n  function (res, next) {\n    process.nextTick(function () {\n      next(null, res - 1, res + 1)\n    })\n  }\n)(1)\n.then(function (res) {\n  // [ 2, 4 ]\n  console.log(res)\n})\n\n```\n\n#### Runner.prototype.sequence(tasks)\nRun `tasks` in sequence.\n\n**NOTE**: directly nested array of tasks will be run with `Runner.prototype.parallel`.\n\n```javascript\nvar runner = Runner()\n\nrunner.sequence([\n  function () { console.log(1) },\n  [\n    function (cb) {\n      setTimeout(function() {\n        console.log(3)\n        cb()\n      }, 0)\n    },\n    function () {\n      return new Promise(function (resolve) {\n        process.nextTick(function () {\n          console.log(2)\n          resolve()\n        })\n      })\n    },\n  ],\n  function () { console.log(4) },\n]).then(function () {\n  console.log('DONE')\n})\n\n// 1\n// 2\n// 3\n// 4\n// DONE\n\n```\n\nCallbacks can be added dynamically:\n\n```javascript\nvar runner = Runner()\n\nvar count = 5\nvar tasks = []\n\nvar res = []\nfunction task(next) {\n  process.nextTick(function () {\n    res.push(count)\n    if (--count \u003e 0) {\n      tasks.push(task)\n    }\n    next()\n  })\n}\nrunner.sequence(tasks).then(function () {\n  // [5, 4, 3, 2, 1]\n  console.log(res)\n})\n\ntasks.push(task)\n\n```\n\n#### Runner.prototype.parallel(tasks)\nRun the specified tasks in parallel.\n\n**NOTE**: directly nested array of tasks will be run with `Runner.prototype.sequence`.\n\n```javascript\nvar runner = Runner()\n\nvar res = []\nrunner.parallel([\n  function () { res.push(1) },\n  [\n    function () {\n      return Promise.resolve().then(function () {\n        res.push(4)\n      })\n    },\n    function () { res.push(5) },\n  ],\n  function (cb) {\n    setTimeout(function() {\n      res.push(3)\n      cb()\n    }, 0)\n  },\n  function (cb) {\n    res.push(2)\n    cb()\n  },\n])\n.then(function () {\n  // [1, 2, 4, 5, 3]\n  console.log(res)\n})\n\n```\n\n#### Runner.prototype.series(...tasks)\nRun `tasks` in sequence.\n\nHowever, while the results of `sequence` is that of the last task,\nthe results of `series` is an array containing all results of the tasks.\n\nIn addition, the results of the previous task will not be passed to the next as arguments.\n\n**NOTE**: each element will be passed to `Runner.prototype.sequence`.\n\n```javascript\nvar runner = Runner()\n\nrunner.series(\n  function () {\n    console.log(1)\n    return 1\n  },\n  [\n    function () {\n      return Promise.resolve().then(function () {\n        console.log(4)\n        return 4\n      })\n    },\n    function (cb) {\n      setTimeout(function() {\n        console.log(3)\n        cb(null, 3)\n      }, 0)\n    },\n    function () {\n      console.log(5)\n      return 5\n    },\n  ],\n  function (cb) {\n    console.log(2)\n    cb(null, 2)\n  }\n)\n.then(function (res) {\n  // 1\n  // 5\n  // 4\n  // 3\n  // 2\n  // [ [ 1  ], [ [ 4  ], [ 3  ], [ 5  ]  ], [ 2  ]  ]\n  console.log(res)\n})\n\n```\n\n\n## [Changelog](changelog.md)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoubin%2Fcallback-sequence","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzoubin%2Fcallback-sequence","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoubin%2Fcallback-sequence/lists"}