{"id":28248658,"url":"https://github.com/lexriver/zen-then","last_synced_at":"2025-06-13T10:30:42.980Z","repository":{"id":36836004,"uuid":"41142909","full_name":"LexRiver/zen-then","owner":"LexRiver","description":"javascript control-flow tool","archived":false,"fork":false,"pushed_at":"2016-03-25T18:20:08.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-24T09:16:21.725Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LexRiver.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":"2015-08-21T07:47:34.000Z","updated_at":"2016-03-25T18:20:09.000Z","dependencies_parsed_at":"2022-09-17T17:51:30.558Z","dependency_job_id":null,"html_url":"https://github.com/LexRiver/zen-then","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LexRiver/zen-then","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LexRiver%2Fzen-then","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LexRiver%2Fzen-then/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LexRiver%2Fzen-then/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LexRiver%2Fzen-then/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LexRiver","download_url":"https://codeload.github.com/LexRiver/zen-then/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LexRiver%2Fzen-then/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259627106,"owners_count":22886791,"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":"2025-05-19T12:16:43.126Z","updated_at":"2025-06-13T10:30:42.971Z","avatar_url":"https://github.com/LexRiver.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zen-then\njavascript control-flow tool that allows execution of async code step by step.\n\n## How to use\n```javascript\nvar z = require('zen-then')();\n\nz.then(function(){\n    console.log('1...');\n    return z.result('one');\n\n}).then(function(x){\n    console.log('2...');\n    console.log('1st function result: ', x);\n    setTimeout(function(){\n        console.log('x=', x);\n        return z.result(x+' plus one');\n    }, 1000);\n\n}).then(function(x){\n    console.log('3...');\n    console.log('result from second function: ', x);\n    return z.result();\n\n});\n```\n\nIt's important to explicitly call `return z.result(...)` after each function. \n```javascript\nreturn z.result('some return value');\nreturn z.result('string',123,x); //few values can be returned\nreturn z.result(); //just continue to the next function\n```\n\n\nTo use returned values in the next function:\n```javascript\nvar z = require('zen-then')();\nz.then(function(){\n    return z.result(1,2,3);\n\n}).then(function(a,b,c){\n    console.log(a,b,c);\n    return z.result();\n    \n});\n\n```\n\n\nThere are some aliases for z.result():\n```javascript\nreturn z.result(...);\nreturn z.ok(...); //same as z.result(...);\nreturn z.return(...); //same as z.result(...);\n```\n\n\n## Exception handling\nExecute `return z.exception(myExceptionObject)` to throw exception and stop execution. \nThis is not the same exception as in `throw myException`.\nUse `.catch(function(exception){ ... })` method to catch exception from any function.\n```javascript\nvar z = require('zen-then')();\n\nz.then(function(){\n    console.log('1...');\n    return z.result('one');\n\n}).then(function(x){\n    console.log('2...');\n    console.log('1st function result: ', x);\n    setTimeout(function(){\n        console.log('x=', x);\n        return z.exception('X'); //let's throw exception\n    }, 1000);\n\n}).then(function(x){\n    // this function will not be executed\n    console.log('3...');\n    console.log('result from second function: ', x);\n    return z.result();\n\n}).catch(function(exception){\n    console.log('catching exception: ', exception);\n\n});\n```\nMethod `.catch(function(exception){ ... }` can catch usual exception also, but only for synchronous functions. \nSo you must exlicitly call `return z.exception(...)` when hadling usual exception in asynchronous functions.\n\nAliases for `return  z.exception(...)`.\n```javascript\nreturn z.exception(x);\nreturn z.error(x); //same\nreturn z.fail(x); //same\n```\n\nAlias for `.catch` is `.onError`:\n```javascript\n.catch(function(exception){ ... });\n.onError(function(exception){ ... }); //same\n```\n\n\n## Warnings\nYou can throw warning messages with `z.warning('message')` and catch them later with \n`.eachWarning(function(warning){ ... })` or with `.getAllWarnings(function(allWarnings){ ... })`.\nAll warnings handling occurs after all functions will be executed or before any exception handling.\n\n```javascript\nvar z = require('zen-then')();\n\nz.then(function(){\n    console.log('1...');\n    z.warning('my warning');\n    return z.result('one');\n\n}).then(function(x){\n    console.log('2...');\n    console.log('1st function result: ', x);\n    setTimeout(function(){\n        console.log('x=', x);\n        z.warning('my another warning');\n        return z.result(x+' plus one');\n    }, 1000);\n\n}).then(function(x){\n    console.log('3...');\n    console.log('result from second function: ', x);\n    return z.result();\n\n}).catch(function(exception){\n    console.log('catching exception: ', exception);\n\n}).eachWarning(function(w){\n    console.log('got warning: ', w);\n\n}).getAllWarnings(function(allWarnings){\n    console.log('all warnings:');\n    for(var i in allWarnings){\n        console.log(' - ' + allWarnings[i]);\n    }\n});\n```\n\n\n## Named functions\nYou can use named functions\n```javascript\nvar z = require('zen-then')();\n\nz.then(function myFirstFunction(){\n    return z.result(1);\n\n}).then(function mySecondFunction(x){\n    console.log(x);\n    return z.result();\n    \n});\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flexriver%2Fzen-then","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flexriver%2Fzen-then","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flexriver%2Fzen-then/lists"}