{"id":15809254,"url":"https://github.com/zordius/subtask.js","last_synced_at":"2025-03-31T23:44:49.813Z","repository":{"id":21970544,"uuid":"25295325","full_name":"zordius/subtask.js","owner":"zordius","description":"An extended promise","archived":false,"fork":false,"pushed_at":"2014-11-30T14:36:59.000Z","size":724,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-06T03:21:42.037Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.org/package/subtask","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zordius.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}},"created_at":"2014-10-16T09:30:14.000Z","updated_at":"2015-03-16T06:33:26.000Z","dependencies_parsed_at":"2022-08-20T03:21:51.250Z","dependency_job_id":null,"html_url":"https://github.com/zordius/subtask.js","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fsubtask.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fsubtask.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fsubtask.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fsubtask.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zordius","download_url":"https://codeload.github.com/zordius/subtask.js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246558113,"owners_count":20796696,"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-10-05T03:21:02.477Z","updated_at":"2025-03-31T23:44:49.792Z","avatar_url":"https://github.com/zordius.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"subtask.js\n==========\n\nAn extended promise.\n\n[![npm version](https://img.shields.io/npm/v/subtask.svg)](https://www.npmjs.org/package/subtask) [![Dependency Status](https://david-dm.org/zordius/subtask.js.png)](https://david-dm.org/zordius/subtask.js)  [![Build Status](https://travis-ci.org/zordius/subtask.js.svg?branch=master)](https://travis-ci.org/zordius/subtask.js) [![Test Coverage](https://codeclimate.com/github/zordius/subtask.js/badges/coverage.svg)](https://codeclimate.com/github/zordius/subtask.js) [![Code Climate](https://codeclimate.com/github/zordius/subtask.js/badges/gpa.svg)](https://codeclimate.com/github/zordius/subtask.js) [![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE.txt)\n\nFeatures\n--------\n\n* .pick(path) to pick required data safely\n* create promise with object and executed with key/value\n\n** Now subtask.js is built on top of promise and changed a lot from old version. If you wanna check old API, please check \u003ca href=\"https://github.com/zordius/subtask.js/tree/none_promise_subtask\"\u003eanother branch\u003c/a\u003e.\n\nHow to Use\n----------\n\n**Define task**\n\n* A task is created by `task creator` function; the function will take your input parameters then return the created task instance (also a promise).\n\n```javascript\nvar task = require('subtask'),\n\n// multiply is a task creator to do sync jobs\n// multiply(1, 2) is a created task instance\nmultiply = function (a, b) {\n    return task(a * b);\n},\n\n// plus is a task creator to do async jobs\n// plus(3, 4) is a created task instance\nplus = function (a, b) {\n    return task(function (resolve) {\n        mathApi.plus(a, b, function (value) {\n            resolve(value);\n        });\n    });\n};\n```\n\n**Execute task**\n\n* Use `.then()` promise api.\n\n```javascript\nmultiply(3, 5).then(function (R) {\n    console.log('3 * 5 = ' + R);\n});\n\nplus(4, 6).then(function (R) {\n    console.log('4 + 6 = ' + R);\n});\n\nvar P35 = plus(3, 5);\n\nP35.then(function (R) {\n    console.log('3 * 5 = ' + R);\n});\n\nP35.then(function (R) {\n    console.log('3 * 5 still = ' + R + ', mathApi.plus only be executed once');\n});\n```\n\n**Parallel subtasks**\n\n* Use object or array to define subtasks.\n* `task.then()` will trigger all subtasks.then() in parallel.\n* Results of all subtasks .then() will be collected back.\n\n```javascript\nvar mathLogic = function (a, b) {\n    return task({\n        multiply: multiply(a, b),\n        plus: puls(a, b),\n        minus: minus(a, b)\n    });\n});\n\nmathLogic(9, 8).then(function (R) {\n    // R will be {multiply: 72, plus: 17, minus: 1}\n});\n```\n\n**Pipe the tasks**\n\n* Use the result of previous task as input of next task creator\n\n```javascript\nvar taskQueue = function (input) {\n    return firstTask(input).then(secondTask).then(thirdTask);\n});\n```\n\n**Transform results**\n\n* Use .pick('path.to.value') to pick wanted value\n\n```javascript\n// when get the title of first story\n// Same with task2(456).then(function (R) {return R.story[0].title});\ntask2(456).pick('story.0.title');\n```\n\nThe Long Story\n--------------\n\n**Serve a page**\n\nWith \u003ca href=\"http://expressjs.com/\"\u003eExpress\u003c/a\u003e, we do this:\n\n```javascript\napp.get('/', function (req, res) {\n   res.send('The page content...');\n});\n```\n\n**Modulize the page**\n\nWe can make the page standalone, then we can mount the page to anywhere.\n\n```javascript\n// The page\nvar somePage = function (req, res) {\n    res.send('The page content...');\n});\n\n\n// Mount it\napp.get('/some/where', somePage);\n```\n\n**Modules in the page**\n\nWe always do this for a page, right?\n\n```javascript\nvar somePage = function (req, res) {\n    var header = getHeaderModule(),\n        body = getStoryModule() + getRelatedStoryModule(),\n        footer = getFooterModule();\n\n    res.send(TemplateEngine(header, body, footer));\n});\n```\n\n**We should provide input for modules**\n\nBut, how do we get the data? By the query parameters? We decide to make modules handle itself.\n\n```javascript\nvar somePage = function (req, res) {\n    var header = getHeaderModule(req),\n        body = getStoryModule(req) + getRelatedStoryModule(req),\n        footer = getFooterModule(req);\n\n    res.send(TemplateEngine(header, body, footer));\n});\n\nvar someModule = function (req) {\n    var id = req.params.id || defaultId,\n        page = getPage(req),\n        ....\n});\n```\n\n* ISSUE 1: many small pieces of code do similar tasks for input.\n* ISSUE 2: the real life of a page is async.\n\n**Everything should be Async**\n\nYes, it's our real life.\n\n```javascript\nvar somePage = function (req, res) {\n    getHeaderModule(req, function(header) {\n        getStoryModule(req, function(body) {\n            getFooterModule(req, function(footer) {\n               res.send(TemplateEngine(header, body, footer));\n            });\n        });\n    });\n});\n```\n\nWe can use \u003ca href=\"https://www.google.com/search?q=javascript+promise\"\u003epromise\u003c/a\u003e to prevent callback hell.\n\n**Parallel is better**\n\nFor performance, maybe we can get modules in parallel? For this we should change the interfaces of modules a bit, make then return a function.\n\n```javascript\nvar somePage = function (req, res) {\n    async.parallel([\n        getHeaderModule(req),\n        getStoryModule(req),\n        getFooterModule(req)\n    ], function (R) {\n        res.send(TemplateEngine(R[0], R[1], R[2]));\n    });\n```\n\n**Take care of Response**\n\nMaybe the `getStoryModule` wanna set cookie? So we should send `req` to all modules...\n\n```javascript\nvar somePage = function (req, res) {\n    async.parallel([\n        getHeaderModule(req, res),\n        getStoryModule(req, res),\n        getFooterModule(req, res)\n    ], function (R) {\n        res.send(TemplateEngine(R[0], R[1], R[2]));\n    });\n```\n\n**Use one Object**\n\nStop appending input parameters, we use one object to handle all requirements.\n\n```javascript\nvar somePage = function (req, res) {\n    var CX = {\n        req: req,\n        res: res\n    };\n\n    async.parallel([\n        getHeaderModule(CX),\n        getStoryModule(CX),\n        getFooterModule(CX)\n    ], function (R) {\n        res.send(TemplateEngine(R[0], R[1], R[2]));\n    });\n```\n\n**How about Title?**\n\nHmmm...In most case, the title is story title. Do it....\n\n```javascript\nvar somePage = function (CX) {\n    async.parallel([\n        getStoryTitle(CX),\n        getHeaderModule(CX),\n        getStoryModule(CX),\n        getFooterModule(CX)\n    ], function (R) {\n        res.send(TemplateEngine(R[0], R[1], R[2], R[3]));\n    });\n```\n\nStop! `R[0]` to `R[n]` are bad! And, why we get the story two times? (one for the page title, another one for the story module) . Maybe we should reuse the fetched data and store it.\n\n**Namespace**\n\nWe can store data in the context `CX`, and define good namespace rule. And we make a framework to handle modules and page. Now the code seems better:\n\n```javascript\nframework.defindPage('somePage', function (CX) {\n    CX.getData('storyTitle').then(function () {\n        CX.getModule(CX, ['header', 'story', 'footer']).then(function () {\n            CX.render('someTemplate', {\n                title: CX.data.story.title,\n                header: CX.module.header,\n                body: CX.module.story,\n                footer: CX.module.footer\n            });\n        });\n    });\n});\n```\n\nNamespace rule is hard to maintain:\n\n* CX.data.stories: a list of stories. Good for all pages.\n* CX.data.user.name: user name. Good for all pages.\n* CX.module.story: story module...What happened when I put 2 story modules in 1 page?!!!\n\nWe do not believe all developers in the team remember all naming rules.\n\n**Make it local**\n\nStop using namespace, it is a 'global variable' solution under `CX`. We should use local variable.\n\n```javascript\nframework.definePage('somePage', function (CX) {\n    CX.executeJobs({\n        title: CX.getData('storyTitle'),\n        header: CX.getModule('header'),\n        body: CX.getModule('story'),\n        footer: CX.getModule('footer')\n    }, function (data) {\n       CX.render('someTemplate', data);\n    });\n});\n```\n\n\u003ca href=\"https://github.com/zordius/subtask.js\"\u003esubtask\u003c/a\u003e is created for this coding style.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzordius%2Fsubtask.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzordius%2Fsubtask.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzordius%2Fsubtask.js/lists"}