Seeking advice on the most efficient method for calling async functions on an array of values in JavaScript.
It's important to note that my current environment does not support the use of async/await methods or promises.
For instance, I have a SHA256 encryption function like this:
sha256(not_encrypted_string, function(encrypted_string) {
// do stuff
});
The goal is to encrypt all values in an array of unspecified length:
const strings_i_want_to_hash = ["string1", "string2", "string3", "string4", "string5", ...];
So the question at hand is: what is the most effective way to hash all these values without using something like
const hashed_strings = strings_i_want_to_hash.map(sha256);
...since it involves asynchronous operations. Correct?
One approach could be to create an empty array to store the hashed strings and wait until it matches the length of the input array:
const hashed_strings = [];
strings_i_want_to_hash.forEach(function(str){
sha256(str, function(hashed_str) {
hashed_strings.push(hashed_str);
});
});
while (hashed_strings.length < strings_i_want_to_hash.length) {
continue;
}
However, this method seems quite inefficient. Are there better alternatives to handle this task?