{"id":17857258,"url":"https://github.com/jilizart/sequence-as-promise","last_synced_at":"2025-03-20T14:31:07.012Z","repository":{"id":69708364,"uuid":"69444357","full_name":"JiLiZART/sequence-as-promise","owner":"JiLiZART","description":"Executes array of functions as sequence and returns promise","archived":false,"fork":false,"pushed_at":"2017-08-24T14:14:04.000Z","size":18,"stargazers_count":22,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-26T09:12:17.926Z","etag":null,"topics":["functions","promise","sequence"],"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/JiLiZART.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-28T08:49:20.000Z","updated_at":"2025-01-18T12:22:52.000Z","dependencies_parsed_at":"2023-06-11T03:23:30.622Z","dependency_job_id":null,"html_url":"https://github.com/JiLiZART/sequence-as-promise","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":"0.23529411764705888","last_synced_commit":"e288bba62843cab288970854633c9804ba14d768"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiLiZART%2Fsequence-as-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiLiZART%2Fsequence-as-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiLiZART%2Fsequence-as-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiLiZART%2Fsequence-as-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JiLiZART","download_url":"https://codeload.github.com/JiLiZART/sequence-as-promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244075615,"owners_count":20393979,"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":["functions","promise","sequence"],"created_at":"2024-10-28T03:12:06.761Z","updated_at":"2025-03-20T14:31:06.713Z","avatar_url":"https://github.com/JiLiZART.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/JiLiZART/sequence-as-promise.svg?branch=master)](https://travis-ci.org/JiLiZART/sequence-as-promise)\n[![Code Climate](https://codeclimate.com/github/JiLiZART/sequence-as-promise/badges/gpa.svg)](https://codeclimate.com/github/JiLiZART/sequence-as-promise)\n## Sequence as Promise\nIt's zero dependency and lightweight function that allows execute array of functions and promises in sequence and returns Promise\n\n## What it do?\nBehavior very similar to `Promise.all`.\nIt's executes promises with functions one by one and returns promise with array of results\n\nthis code\n```js\npromise1.then(() =\u003e promise2.then(() =\u003e promise3.then(callback))).then(done);\n```\n\nequivalent to\n```js\nconst sequence = require('sequence-as-promise');\n\nsequence([promise1, promise2, promise3, callback]).then(done);\n```\n\n## How to install\n\nwith npm\n```shell\nnpm i --save sequence-as-promise\n```\n\nwith yarn\n```shell\nyarn add sequence-as-promise\n```\n\n## Basic usage\n\nWe have array of functions with promises, and we need to execute all that functions in sequence\n\nAll functions in sequence accepts two arguments, `(prevResult, results) =\u003e {}`\n\n- `prevResult` the result of previous function or promise call\n- `results` an array of results from previous functions or promises calls\n\n```js\nconst sequence = require('sequence-as-promise');\nsequence([\n    Promise.resolve({status: true}),\n    (prevResult/*{status: true}*/, results) =\u003e {\n        return {moveCircleToMiddle: true};\n    },\n    (prevResult/*{moveCircleToMiddle: true}*/, results) =\u003e {\n        return {showGrayCircle: true};\n    },\n    (prevResult/*{showGrayCircle: true}*/, results) =\u003e {\n        return {showMicrophone: true};\n    }\n]).then((results) =\u003e console.log('all done', results))\n```\n\n## Functions that returns promise\n\nMost standard use case is a fetch dependant data one by one\n\n```js\nconst sequence = require('sequence-as-promise');\nsequence([\n    fetchUser(32),\n    (user) =\u003e {\n        if (user \u0026\u0026 user.id === 1) {\n            return fetchAdminUrls(user.id);\n        }\n\n        return fetchUserUrls(user.id);\n    },\n    (urls) =\u003e {//previous fetch resolved and passed as argument\n        return urls.map(makeLink)\n    }\n]).then((results) =\u003e {\n    const [user, _, links] = results;\n    \n    renderHTML(user, links);\n});\n```\n\n## Handle errors\n\nAny function or promise in sequence can throw an error, so we need to handle it\n\n```js\nconst sequence = require('sequence-as-promise');\nsequence([\n    fetchUser(32),\n    (user) =\u003e {\n        if (user \u0026\u0026 user.id === 1) {\n            return fetchAdminUrls(user.id); //for instance this fetch throws server error\n        }\n\n        return fetchUserUrls(user.id);\n    },\n    (urls) =\u003e { //this will not be executed, because previous promise thorws error\n        return urls.map(makeLink); \n    }\n]).then(\n    (results) =\u003e {\n        const [user, _, links] = results;\n        \n        renderHTML(user, links);\n    },\n    (results) =\u003e {\n        const error = results.pop(); //last item in results always be an error\n\n        renderError(error);\n    }\n);\n```\n\n## More examples\nBut, if we need to call all that functions with primitive values between them (why not?).\n\n```js\nconst sequence = require('sequence-as-promise');\nsequence([\n    () =\u003e {\n        return {moveCircleToMiddle: true};\n    },\n    100,\n    (prevResult/*100*/, results) =\u003e {\n        return {showGrayCircle: prevResult};\n    },\n    (prevResult/*{showGrayCircle: 100}*/, results) =\u003e {\n        return {showMicrophone: true};\n    },\n    500,\n    (prev, values) =\u003e { // prev == 500\n        return {moveCircleToTop: true};\n    }\n]).then((results) =\u003e console.log('all done', results))\n```\n\nOr we have Promises in that array of functions.\n\n```js\nconst sequence = require('sequence-as-promise');\nsequence([\n    () =\u003e new Promise((resolve, reject) =\u003e {\n        resolve({moveCircleToMiddle: true});\n    }),\n    () =\u003e {\n        return {showGrayCircle: true};\n    }\n]).then(() =\u003e console.log('all done'))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjilizart%2Fsequence-as-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjilizart%2Fsequence-as-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjilizart%2Fsequence-as-promise/lists"}