Javascript 异步映射提早完成
Javascript async map completing early
我正在尝试为数组中的每个项目异步查询 Firebase Cloud Firestore。我对 async.map 函数的理解是,它将对数组中的每个项目执行一个函数,并且仅在处理完所有项目后才触发其回调。但是,下面的回调会在第一个查询之后立即执行,并且在任何结果可用之前执行。执行此操作的最佳方法是什么?
var db = admin.firestore();
var getData = function (item, doneCallback) {
db.collection('myCollection').doc(item).get()
.then(results => {
console.log("Callback here");
return doneCallback({
"name": results.data().name,
"id": results.data().id,
"phone": results.data().phone,
});
});
};
async.map(myArray, getData, function (err, results) {
console.log("Finished");
for (var i=0;i<results.length; i+=1){
console.log(results[i].name);
console.log(results[i].id);
console.log(results[i].phone);
}
});
问题中的代码没有 return
来自 getData()
的 Promise
调用,参见
var getData = function(item, doneCallback) {
// `return` the `Promise` object from the function
return db.collection('myCollection').doc(item).get()
.then(results => {
console.log("Callback here");
return doneCallback({
"name": results.data().name,
"id": results.data().id,
"phone": results.data().phone,
});
});
};
另一种避免async.js的方法(我个人觉得回调代码很难读懂,promises最终会更加统一):
var db = admin.firestore();
var promises = [];
for (var i = 0; i < myArray.length; i++){
var promise = db.collection('myCollection').doc(myArray[i]).get()
.then(results => {
console.log("Callback here");
return {
"name": results.data().name,
"id": results.data().id,
"phone": results.data().phone,
};
});
promises.push(promise);
}
Promise.all(promises).then(results, () => {
console.log("Finished");
for (var i = 0; i < results.length; i++){
console.log(results[i].name);
console.log(results[i].id);
console.log(results[i].phone);
}
});
我正在尝试为数组中的每个项目异步查询 Firebase Cloud Firestore。我对 async.map 函数的理解是,它将对数组中的每个项目执行一个函数,并且仅在处理完所有项目后才触发其回调。但是,下面的回调会在第一个查询之后立即执行,并且在任何结果可用之前执行。执行此操作的最佳方法是什么?
var db = admin.firestore();
var getData = function (item, doneCallback) {
db.collection('myCollection').doc(item).get()
.then(results => {
console.log("Callback here");
return doneCallback({
"name": results.data().name,
"id": results.data().id,
"phone": results.data().phone,
});
});
};
async.map(myArray, getData, function (err, results) {
console.log("Finished");
for (var i=0;i<results.length; i+=1){
console.log(results[i].name);
console.log(results[i].id);
console.log(results[i].phone);
}
});
问题中的代码没有 return
来自 getData()
的 Promise
调用,参见
var getData = function(item, doneCallback) {
// `return` the `Promise` object from the function
return db.collection('myCollection').doc(item).get()
.then(results => {
console.log("Callback here");
return doneCallback({
"name": results.data().name,
"id": results.data().id,
"phone": results.data().phone,
});
});
};
另一种避免async.js的方法(我个人觉得回调代码很难读懂,promises最终会更加统一):
var db = admin.firestore();
var promises = [];
for (var i = 0; i < myArray.length; i++){
var promise = db.collection('myCollection').doc(myArray[i]).get()
.then(results => {
console.log("Callback here");
return {
"name": results.data().name,
"id": results.data().id,
"phone": results.data().phone,
};
});
promises.push(promise);
}
Promise.all(promises).then(results, () => {
console.log("Finished");
for (var i = 0; i < results.length; i++){
console.log(results[i].name);
console.log(results[i].id);
console.log(results[i].phone);
}
});