Firebase云功能:Promise.all问题(promise) [英] Firebase cloud function: problem with Promise.all(promise)
本文介绍了Firebase云功能:Promise.all问题(promise)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我循环抛出Firebase存储中的文件列表,我想在循环时修改字符串,
I loop throw a list of files in Firebase storage and I would like to modify a string while looping,
这是我尝试做的事情:
var str;
storage.bucket().file(...).download((err, content) => {
str=content.toString();
storage.bucket().getFiles(...).then(results => {
const files = results[0];
var promise = new Promise(function(resolve,reject){
files.forEach(file => {
...
str=str.replace("t","a");
});
resolve(str);
});
Promise.all(promise).then(function(str) {
console.log(str); //NOT OKAY, the value is still "test"
file.save(str, function(err) { ... });
});
我也尝试过:
promise.then(function(result){
promise.then(function(result) {
但结果相同:(
更新:我已经编辑了上面的代码,但仍然无法正常工作:
UPDATE : I edited the code above but it still doesn't work :
有什么主意吗?
更新2:
它仍然不起作用:(
推荐答案
如果对某人有用,这是我找到的解决方案:
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
});
});
这篇关于Firebase云功能:Promise.all问题(promise)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文