【发布时间】:2022-01-04 01:27:24
【问题描述】:
我有以下代码可以从 Firebase 中检索一些文件信息:
function loadUserProfileKeys(key) {
// Get the Firebase storage ref for the InitialUserProfiles folder
var storageRef = firebase.storage().ref();
var initialUserProfilesRef = storageRef.child('InitialUserProfiles'); // .txt files folder
// Array
const keyResults = [];
// Retrieve all profiles
initialUserProfilesRef.listAll()
.then(function(res) {
// Loop over each item
for (const itemRef of res.items) {
console.log("Start for loop");
// Ignore profiles with symbols (workaround - TODO: fix this)
if (/,|&/.test(itemRef.name)) {} else {
// Path of the file
var pathRef = initialUserProfilesRef.child(itemRef.name);
// Get the file's download URL
var downloadURL = pathRef.getDownloadURL()
.then((url) => {
// Get the given key from the user profile text file
getValueKey(url, key)
.then((value) => {
// Add it to the keyResults array
keyResults.push(value);
});
});
}
}
console.log("End for loop");
console.log(keyResults);
}).catch((error) => {
console.log("ERROR");
console.log(error);
});
}
async function getValueKey(fileURL, key) {
let response = await fetch(fileURL);
if (response.status == 200) {
let json = await response.text(); // (3)
var lines = json.split("\n");
var results = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var pairs = line.split(":");
if (pairs.length == 2 && pairs[0].trim() == key) {
results.push(pairs[1].trim());
}
}
return Promise.resolve(results[0]);
}
}
日志本身很好 - 在所有循环(即多个“开始循环”日志)完成之前,它不会记录“循环结束”。
问题是这仍然是 在 keyResults.push(value); 被调用之前 - 因此数组是空的(或者偶尔只是部分填充)。
如何让 var downloadURL = pathRef.getDownloadURL() 和 getValueKey(url, key) 阻塞 - 以便在调用嵌套的 .then((value) 添加到数组之前它不会遍历循环?
我无法弄清楚异步等 - 我不断收到语法错误。
function loadUserProfileKeys(key) {
// Get the Firebase storage ref for the InitialUserProfiles folder
var storageRef = firebase.storage().ref();
var initialUserProfilesRef = storageRef.child('InitialUserProfiles'); // .txt files folder
const keyResults = Promise.all(initialUserProfilesRef.listAll().then(function(res) {
// Loop over each item
return res.items.map((itemRef) => {
// Ignore profiles with symbols (workaround - TODO: fix this)
if (/,|&/.test(itemRef.name)) {} else {
// Path of the file
var pathRef = initialUserProfilesRef.child(itemRef.name);
// Get the file's download URL
return pathRef.getDownloadURL()
.then((url) => {
// Get the given key from the user profile text file
return getValueKey(url, key)
});
};
});
}));
console.log("End for loop");
console.log(keyResults);
}
async function getValueKey(fileURL, key) {
let response = await fetch(fileURL);
if (response.status == 200) {
let json = await response.text(); // (3)
var lines = json.split("\n");
var results = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var pairs = line.split(":");
if (pairs.length == 2 && pairs[0].trim() == key) {
results.push(pairs[1].trim());
}
}
return Promise.resolve(results[0]);
}
}
【问题讨论】:
-
没有办法让 Promise 阻塞。你可能不得不使用回调。
-
我对 firebase 一无所知,但async function 可能会有所帮助。
标签: javascript firebase firebase-storage