To achieve the task of running 5 promises, storing their input in an object, and calling a function once all of them are complete in an ES5 environment, you can utilize the Promise.all() method. However, since Promise.all() is a part of ES6, we'll have to implement a similar functionality manually or use a library like Bluebird or Q for promise handling. For the sake of this example, I'll demonstrate how to achieve this using native ES5 with a simple implementation of Promise.all().
First, let's create a simple Promise.all() polyfill for ES5:
function PromiseAll(promises) {
var results = {};
var count = 0;
var total = Object.keys(promises).length;
var deferred = {};
deferred.promise = new Promise(function(resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
function checkComplete() {
count++;
if (count === total) {
deferred.resolve(results);
}
}
for (var key in promises) {
if (promises.hasOwnProperty(key)) {
promises[key].then(function(result) {
results[key] = result;
checkComplete();
}).catch(deferred.reject);
}
}
return deferred.promise;
}
However, the above PromiseAll function uses the native Promise constructor which is not fully ES5. To be more accurate to ES5, we should note that Promises are not natively supported in ES5. But, assuming you have a promise library or a polyfill like the one above, here's how you can proceed.
Let's say you have 5 functions that return promises, and you want to store their results in an object and then call another function once all of them are resolved.
// Example promise-returning functions
function task1() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Task 1 result");
}, 1000);
});
}
function task2() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Task 2 result");
}, 2000);
});
}
function task3() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Task 3 result");
}, 500);
});
}
function task4() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Task 4 result");
}, 1500);
});
}
function task5() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Task 5 result");
}, 3000);
});
}
// Creating an object with promises
var promises = {
task1: task1(),
task2: task2(),
task3: task3(),
task4: task4(),
task5: task5()
};
// Using PromiseAll to wait for all promises to resolve
PromiseAll(promises).then(function(results) {
console.log(results);
// Call your function here with the results
yourFunction(results);
}).catch(function(error) {
console.error("An error occurred:", error);
});
// Example function to be called after all promises are resolved
function yourFunction(results) {
console.log("All tasks completed. Results:", results);
// Process the results here
}
In this example, PromiseAll is used to wait for all the promises in the promises object to resolve. Once all are resolved, it logs the results and calls yourFunction with the results. If any of the promises reject, it catches the error and logs it.
Keep in mind, for a full ES5 compatibility without native Promises, you would typically use a library like jQuery (with its Deferred objects) or a dedicated promise library. The example above assumes some form of promise support.