Javascript forEach循环内的XMLHTTPRequest不';行不通
大家好,我正在编写一个简短的node.js应用程序,每次遍历数组时都会向API发送XMLHTTPRequest。问题是,由于异步性质,它在返回请求之前继续foreach循环。我可能忽略了一些重要的事情,但今天下午的大部分时间我都在绞尽脑汁。我尝试过使用Wait,但没有效果,任何解决方案都将不胜感激 提前谢谢 节点JS应用程序Javascript forEach循环内的XMLHTTPRequest不';行不通,javascript,node.js,foreach,xmlhttprequest,Javascript,Node.js,Foreach,Xmlhttprequest,大家好,我正在编写一个简短的node.js应用程序,每次遍历数组时都会向API发送XMLHTTPRequest。问题是,由于异步性质,它在返回请求之前继续foreach循环。我可能忽略了一些重要的事情,但今天下午的大部分时间我都在绞尽脑汁。我尝试过使用Wait,但没有效果,任何解决方案都将不胜感激 提前谢谢 节点JS应用程序 const mongoose = require("mongoose"); const fs = require("fs"); const ajax = require("
const mongoose = require("mongoose");
const fs = require("fs");
const ajax = require("./modules/ajax.js");
// Bring in Models
let Dictionary = require("./models/dictionary.js");
//=============================
// MongoDB connection
//=============================
// Opens connection to database "test"
mongoose.connect("mongodb://localhost/bookCompanion");
let db = mongoose.connection;
// If database test encounters an error, output error to console.
db.on("error", (err)=>{
console.console.error("Database connection failed.");
});
// Check for connection to the database once.
db.once("open", ()=>{
console.info("Connected to MongoDB database...");
fs.readFile("./words.json", "utf8", (err, data)=>{
if(err){
console.log(err);
} else {
data = JSON.parse(data);
data.forEach((word, index)=>{
let search = ajax.get(`LINK TO API?=${word}`);
search.then((response)=>{
let newWord = new Dictionary ({
Word: response.word,
phonetic: response.phonetic,
meaning: response.meaning
}).save();
console.log(response);
}).catch((err)=>{
console.log(err);
});
});
}
});
});
XMLHTTPRequest模块
// Get Request module utilising promises
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const get = (url)=>{
// This function will return a promise, promises use resolve and reject. The resolve is accessed through .then and the reject through the .catch
return new Promise((resolve, reject)=>{
// Create new XMLhttp (AJAX) Request
let xhr = new XMLHttpRequest();
// Sets up the request, setting it to a GET request, pointing to the website and setting it to asynchronous
xhr.open("GET", url , true);
//sends the request
xhr.send();
xhr.onload = ()=>{
if (xhr.status == 200){
// When loaded pass the response over to the .then method
resolve(JSON.parse(xhr.responseText));
} else {
// if error pass the status code to the .catch method for error handling
reject(xhr.statusText);
}
};
xhr.onerror = ()=>{
// if error pass the status code to the .catch method for error handling
reject(xhr.statusText && xhr.status);
};
});
};
module.exports.get = get;
你应该习惯于等待所有的承诺完成。all将承诺数组作为输入,ans等待所有承诺得到解决。如果您使用promise.all,您的代码将是这样的
const mongoose = require("mongoose");
const fs = require("fs");
const ajax = require("./modules/ajax.js");
// Bring in Models
let Dictionary = require("./models/dictionary.js");
//=============================
// MongoDB connection
//=============================
// Opens connection to database "test"
mongoose.connect("mongodb://localhost/bookCompanion");
let db = mongoose.connection;
// If database test encounters an error, output error to console.
db.on("error", (err) => {
console.console.error("Database connection failed.");
});
// Check for connection to the database once.
db.once("open", () => {
console.info("Connected to MongoDB database...");
fs.readFile("./words.json", "utf8", (err, data) => {
if (err) {
console.log(err);
} else {
data = JSON.parse(data);
var promiseArr = []
Promise.all(promiseArr.push(
new Promise((resolve, reject) => {
let search = ajax.get(`LINK TO API?=${word}`);
search.then((response) => {
let newWord = new Dictionary({
Word: response.word,
phonetic: response.phonetic,
meaning: response.meaning
}).save();
console.log(response);
resolve();
}).catch((err) => {
console.log(err);
reject();
});
})
)).then((response) => {
//whatever you want to do after completion of all the requests
})
}
});
});
在处理较小的数组时,我的代码似乎工作得很好,我遇到的真正问题是处理forEach循环和内存的阻塞性质。我需要循环的数组由超过400000个字组成,在forEach循环完成并释放调用堆栈供httprequests解析之前,应用程序内存不足
任何关于如何创建不阻塞调用堆栈的同步forEach循环的信息都将非常感谢。您可以研究使用
Promise.All
或者如果您需要在下一次迭代中使用第一次迭代的响应,您不能使用for循环,而是在每次返回响应时增加索引并执行下一个调用。