{"id":16527825,"url":"https://github.com/benoitzugmeyer/wrapas","last_synced_at":"2025-03-03T05:14:35.365Z","repository":{"id":5410233,"uuid":"6600790","full_name":"BenoitZugmeyer/wrapas","owner":"BenoitZugmeyer","description":"Yet another CPS helper","archived":false,"fork":false,"pushed_at":"2013-06-30T20:19:22.000Z","size":116,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-15T12:17:48.094Z","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/BenoitZugmeyer.png","metadata":{"files":{"readme":"README.rst","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":"2012-11-08T18:14:00.000Z","updated_at":"2018-12-14T10:01:20.000Z","dependencies_parsed_at":"2022-09-26T17:01:26.183Z","dependency_job_id":null,"html_url":"https://github.com/BenoitZugmeyer/wrapas","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fwrapas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fwrapas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fwrapas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fwrapas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BenoitZugmeyer","download_url":"https://codeload.github.com/BenoitZugmeyer/wrapas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241610990,"owners_count":19990508,"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-11T17:37:01.546Z","updated_at":"2025-03-03T05:14:35.346Z","avatar_url":"https://github.com/BenoitZugmeyer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"======\nwrapas\n======\n\nThe ``wrapas`` function is a simple helper to use with continuation passing\nstyle libraries. It helps to resolve some common pattern and problems brought\nby this convention and helps the developer to have a better vision of the\ncontrol flow of his program.\n\nA `lot of alternatives`_ already exist, I did not check them all. There is\nprobably some solutions much more accurate to your needs, but here is mine.\n\n\nFeatures\n--------\n\n* provides a simple way to aggregate results, in a predictable order\n* handles errors at one unique place\n* prevents multiple calls of a callback\n* guarantees that the final callback will be asynchronous\n* binds the callback to a given context\n* ≈576 bytes minified, ≈370 bytes gzipped, nodejs and browser compliant\n\n\nExamples\n--------\n\nCompile a template:\n\n.. code:: javascript\n\n  function compileTemplate(templatePath, dataPath, callback) {\n    var as = wrapas(function (error, template, data) {\n      if (error) {\n        callback(error);\n      }\n      else {\n        template.render(data);\n      }\n    });\n\n    fs.readFile(templatePath, as(function (content) {\n      return new Template(content);\n    }));\n\n    fs.readFile(dataPath, as(function (content) {\n      return JSON.stringify(content);\n    }));\n  }\n\nFetch multiple ressources:\n\n.. code:: javascript\n\n  {\n\n    // getAllIds: function(callback) { .... },\n    // getUser: function(id, callback) { .... },\n\n    getUserNames: function (callback) {\n      var as = wrapas(function (error) {\n        // Transform the arguments to an array\n        callback(error, [].splice.call(arguments, 1));\n      }, this);\n\n      this.getAllIds(as(function (ids) {\n        ids.forEach(function (id) {\n            this.getUser(id, as());\n        }, this);\n      }));\n    }\n  }\n\n\nDependency\n----------\n\nIn order to work with callbacks that could be synchronous, we have to use a\nfunction that will do an asynchronous call just after all synchronous code is\nrun. ``setTimeout(..., 0)`` could be used but it is very slow: the minimum\ndelay is bound to a few milliseconds, depending on the environment.\n\nWe use the ``setImmediate(...)`` alternative, which is only on Internet\nExplorer 10 but that can be implemented on other environments as well with\n``postMessage`` (recent browsers), ``nextTick`` (nodejs) or ``setTimeout`` (as\na fallback).\n\nInclude *immediate.js* if you don't have any better polyfill for this function\n(and its counterpart, ``clearImmediate``).\n\n\nAPI\n---\n\nThe ``wrapas`` function will create a ‘callback registry’. It will returns a\nfunction that, when called, registers a new callback, and return a wrapper\nfunction to be used as a callback in continuation passing style APIs like the\nnodejs standard library.\n\n\n``wrapas(function(error, args...), ?object)``\n`````````````````````````````````````````````\n\nCreate a new ‘callback registry’ in order to track callbacks. The first\nargument is a function to call when all callbacks are executed, or if a\ncallback receive an error. The ``error`` argument is the first error to be\npassed to any callback, and the other arguments are values returned by the\nfunctions you register.\n\nThe second argument is optional and is passed as context for all callbacks.\n\n\n``‘callback registry’(function(args...))``\n``````````````````````````````````````````\n\nRegister a new callback. If the resulting function is called with a truthy\nvalue as first argument, this callback will be called with the other arguments.\nElse, the final callback will be called with the first argument and all future\ncall on functions from the registry will be ignored.\n\nWhen all resulting function are called and there was no error, the final\ncallback is called with the defined values returned by the registered\ncallbacks as arguments.\n\n\nRandom thoughts\n---------------\n\nWrap this library yourself! Chose the `best wrapper`_ that covers your needs.\n\n\n\n\n.. _lot of alternatives: https://github.com/joyent/node/wiki/modules#wiki-async-flow\n.. _best wrapper: https://github.com/umdjs/umd\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenoitzugmeyer%2Fwrapas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenoitzugmeyer%2Fwrapas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenoitzugmeyer%2Fwrapas/lists"}