How to use the all function from when
Find comprehensive JavaScript when.all code examples handpicked from public code repositorys.
when.all is a function that takes an array of promises and returns a new promise that is fulfilled when all of the input promises have been fulfilled.
165 166 167 168 169 170 171 172 173 174
} } } return when.all(promises).then(function() { return gitTools.stageFile(project.path,files); }).then(function() { return gitTools.commit(project.path,"Create project files",getGitUser(user)); }).then(function() {
+ 2 other calls in file
GitHub: MechMAM/SpringReferences
44 45 46 47 48 49 50 51 52 53
method: 'GET', path: employee._links.self.href }) ); }).then(employeePromises => { return when.all(employeePromises); }).done(employees => { this.setState({ page: this.page, employees: employees,
+ 62 other calls in file
How does when.all work?
when.all is a function provided by the when library in JavaScript that takes an array of promises as input and returns a new promise. This new promise will be fulfilled when all of the input promises have been fulfilled, and will be rejected if any of the input promises are rejected. The returned promise will have an array of results from the input promises as its fulfillment value. If the input promises do not return values, the fulfillment value will be an array of undefined. If any of the input promises are rejected, the returned promise will be rejected with the reason of the first rejected promise. when.all also supports an optional mapper argument, which is a function that will be called with each fulfilled value of the input promises. The return value of the mapper function will be used as the corresponding value in the fulfillment value array of the returned promise. This allows for transformation of the input values before they are returned in the final array. It is important to note that the input promises are not executed in any particular order, and the order of the results in the fulfillment value array may not correspond to the order of the input promises.
GitHub: vektoririna/tasks
159 160 161 162 163 164 165 166 167 168
* permissions_users * roles_users */ // Write changes to DB, if successful commit, otherwise rollback // when.all() does not work as expected, when.settle() does. when.settle(ops).then(function (descriptors) { var rej = false, error = ''; descriptors.forEach(function (d) {
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
const when = require("when"); const promise1 = when.resolve("Hello"); const promise2 = when.resolve("world"); const promise3 = when.resolve("!"); const promises = [promise1, promise2, promise3]; when .all(promises) .then((results) => { console.log(results); // Output: ['Hello', 'world', '!'] }) .catch((error) => { console.error(error); });
In this example, we create three promises that immediately resolve to strings. We then add those promises to an array and pass it to when.all. The returned promise will be fulfilled with an array of the resolved values of each input promise, which in this case will be ['Hello', 'world', '!']. We then log this array to the console.