{"id":13685708,"url":"https://github.com/cujojs/when","last_synced_at":"2025-05-12T20:49:06.837Z","repository":{"id":392594,"uuid":"1545869","full_name":"cujojs/when","owner":"cujojs","description":"A solid, fast Promises/A+ and when() implementation, plus other async goodies.","archived":false,"fork":false,"pushed_at":"2022-04-10T19:35:16.000Z","size":2532,"stargazers_count":3436,"open_issues_count":67,"forks_count":394,"subscribers_count":131,"default_branch":"master","last_synced_at":"2025-05-01T18:03:38.171Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/cujojs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-03-30T11:52:09.000Z","updated_at":"2025-05-01T15:37:27.000Z","dependencies_parsed_at":"2022-08-06T09:15:06.732Z","dependency_job_id":null,"html_url":"https://github.com/cujojs/when","commit_stats":null,"previous_names":[],"tags_count":83,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cujojs%2Fwhen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cujojs%2Fwhen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cujojs%2Fwhen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cujojs%2Fwhen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cujojs","download_url":"https://codeload.github.com/cujojs/when/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251993065,"owners_count":21677026,"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-08-02T14:00:56.319Z","updated_at":"2025-05-12T20:49:06.795Z","avatar_url":"https://github.com/cujojs.png","language":"JavaScript","readme":"\u003ca href=\"http://promises-aplus.github.com/promises-spec\"\u003e\u003cimg src=\"http://promises-aplus.github.com/promises-spec/assets/logo-small.png\" alt=\"Promises/A+ logo\" align=\"right\" /\u003e\u003c/a\u003e\n\n[![Build Status](https://travis-ci.org/cujojs/when.svg?branch=master)](https://travis-ci.org/cujojs/when)\n[![Inline docs](http://inch-ci.org/github/cujojs/when.svg?branch=master)](http://inch-ci.org/github/cujojs/when)\n\nwhen.js\n=======\n\nWhen.js is a rock solid, battle-tested [Promises/A+](http://promises-aplus.github.com/promises-spec) and `when()` implementation, including a complete [ES6 Promise shim](docs/es6-promise-shim.md).  It's a powerful combination of small size, high performance, debuggability, and rich features:\n\n* Resolve arrays and hashes of promises, as well as infinite promise sequences\n* Execute tasks in parallel or sequentially\n* Transform Node-style and other callback-based APIs into promise-based APIs\n\nWhen.js is one of the many stand-alone components of [cujoJS](http://cujojs.com), the JavaScript Architectural Toolkit.\n\nCheck it out:\n\n- [What's new](CHANGES.md)\n- [API docs](docs/api.md#api)\n- Read more about how [promises simplify async programming](http://know.cujojs.com/tutorials/async/simplifying-async-with-promises)\n\nInstallation\n------------\n\n#### AMD\n\nAvailable as `when` through [bower](http://bower.io), or just clone the repo and load `when.js` from the root.\n\n```\nbower install --save when\n```\n\n#### CommonJS/Node\n\n```\nnpm install --save when\n```\n\n[More help \u0026 other environments \u0026raquo;](docs/installation.md)\n\nUsage\n-----\n\nPromises can be used to help manage complex and/or nested callback flows in a simple manner. To get a better handle on how promise flows look and how they can be helpful, there are a couple examples below (using commonjs).\n\nThis first example will print `\"hello world!!!!\"` if all went well, or `\"drat!\"` if there was a problem. It also uses [rest](https://github.com/cujojs/rest) to make an ajax request to a (fictional) external service.\n\n```js\nvar rest = require('rest');\n\nfetchRemoteGreeting()\n    .then(addExclamation)\n    .catch(handleError)\n    .done(function(greeting) {\n        console.log(greeting);\n    });\n\nfunction fetchRemoteGreeting() {\n    // convert native Promise to a when.js promise for 'hello world'\n    var result = rest('http://example.com/greeting');\n    return when(result)\n}\n\nfunction addExclamation(greeting) {\n    return greeting + '!!!!'\n}\n\nfunction handleError(e) {\n    return 'drat!';\n}\n```\n\nThe second example shows off the power that comes with when's promise logic. Here, we get an array of numbers from a remote source and reduce them. The example will print `150` if all went well, and if there was a problem will print a full stack trace.\n\n```js\nvar when = require('when');\nvar rest = require('rest');\n\nwhen.reduce(when.map(getRemoteNumberList(), times10), sum)\n    .done(function(result) {\n        console.log(result);\n    });\n\nfunction getRemoteNumberList() {\n    // Get a remote array [1, 2, 3, 4, 5]\n    return rest('http://example.com/numbers').then(JSON.parse);\n}\n\nfunction sum(x, y) { return x + y; }\nfunction times10(x) {return x * 10; }\n```\n\nLicense\n-------\n\nLicensed under MIT. [Full license here \u0026raquo;](LICENSE.txt)\n\nContributing\n------------\n\nPlease see the [contributing guide](CONTRIBUTING.md) for more information on running tests, opening issues, and contributing code to the project.\n\nReferences\n----------\n\nMuch of this code was inspired by the async innards of [wire.js](https://github.com/cujojs/wire), and has been influenced by the great work in [Q](https://github.com/kriskowal/q), [Dojo's Deferred](https://github.com/dojo/dojo), and [uber.js](https://github.com/phiggins42/uber.js).\n","funding_links":[],"categories":["Promises/A+ Implementations (ES6/ES2015 compatible)","JavaScript","Control Flow","Control Flow [🔝](#readme)","控制流"],"sub_categories":["Implementations with extras","Runner","运行器","运行器e2e测试"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcujojs%2Fwhen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcujojs%2Fwhen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcujojs%2Fwhen/lists"}