Firebase Cloud Function: Problem With Promise.all(promise)
I loop throw a list of files in Firebase storage and I would like to modify a string while looping, Here is what I tried to do: var str; storage.bucket().file(...).download((er
Solution 1:
If it can be usefull for someone, here is the solution I found:
var promises = [];
var str="string containing data to replace with signed url";
storage.bucket().getFiles({ prefix: folderPath }).then(results => {
const files = results[0];
files.forEach(function(file) {
promises.push( //the trick was here
file.getSignedUrl(signedUrlConfig).then(signedUrls => {
...
surl = signedUrls[0];
str=str.replace("a",surl); //eg: replace with signed url.return str;
});
);
});
Promise.all(promises).then(() => {
console.log(str); //str contains all signed url
});
});
Solution 2:
It seems you are looking for
const promise = storage.bucket().file().download().then(str => {
// ^^^^^^^^^ ^^^^^return storage.bucket().getFiles().then(results => {
// ^^^^^^const files = results[0];
for (const file of files) {
…
str = str.replace("t","a");
}
returnstr;
// ^^^^^^
});
});
promise.then(str => { /*
^^^^^^^^^^^^ */
console.log(str);
return file.save(str); // should return a promise
});
You need neither new Promise
nor Promise.all
here. You might use the latter though to remove the nesting and potentially even run getFiles()
and download()
concurrently, see How do I access previous promise results in a .then() chain?.
Post a Comment for "Firebase Cloud Function: Problem With Promise.all(promise)"