[英]Javascript for loop variable is strange when writing on a file
我在“./utils/url.js”中有这段代码。 它基本上使application/x-www-form-urlencoded
内容形式:
const ContentForm = ()=>{
let params = new URLSearchParams()
const randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
params.append('email', `${randomString}@gmail.com`)
return params;
}
module.exports = ContentForm;
email 参数是一个随机字符串。
和 index.js:
const axios = require('axios').default;
const fs = require('fs');
const params = require('./utils/url')
for (let i = 0; i < 1000; i++) {
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
// sending post with data of web/application the url http://somewhere.com/my-account/
axios.post('http://somewhere.com/my-account/',params(),config, {
})
.then(function (response) {
console.log("request successfully made")
})
.catch(function (error) {
// seeing the error response code
console.log(error.response.status);
})
.finally(function () {
// always executed
fs.writeFileSync('./r.txt',String(i));
})
}
所以我希望将 'i' 变量写入./r.txt
。 它实际上意味着我们现在发送的请求写入。 但问题是它真的很奇怪: 看看 r.txt 的视频在这里变化
您正在一个循环中运行 1000 个异步操作。 您按顺序启动它们,但它们都并行运行。 然后当每个人完成每个人都会调用fs.writeFileSync()
,这是一场比赛,看哪个人何时调用它。 每个人完成的顺序是随机的,我认为这就是你的视频所显示的。
您可以使用await
对它们进行排序,如下所示:
const axios = require("axios").default;
const fs = require("fs");
const params = require("./utils/url");
async function run() {
for (let i = 0; i < 1000; i++) {
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
};
// sending post with data of web/appliaction the url http://somewhere.com/my-account/
await axios
.post("http://somewhere.com/my-account/", params(), config, {})
.then(function(response) {
console.log("request succesfully made");
})
.catch(function(error) {
// seeing the error response code
console.log(error.response.status);
})
.finally(function() {
// always executed
fs.writeFileSync("./r.txt", String(i));
});
}
}
run().then(() => {
console.log("done");
}).catch(err => {
console.log(err);
});
或者,重新组织一下,不要在同一个 function 中混合await
和 .then .then()
,如下所示:
const axios = require("axios").default;
const fs = require("fs");
const params = require("./utils/url");
async function run() {
for (let i = 0; i < 1000; i++) {
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
};
// sending post with data of web/appliaction the url http://somewhere.com/my-account/
try {
let response = await axios.post("http://somewhere.com/my-account/", params(), config, {});
console.log("request succesfully made");
} catch(error) {
console.log(error.response.status, error);
} finally {
fs.writeFileSync("./r.txt", String(i));
}
}
}
run().then(() => {
console.log("done");
}).catch(err => {
console.log(err);
});
async function writeToFile(){
for (let i = 0; i < 1000; i++) {
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
};
// sending post with data of web/appliaction the url http://somewhere.com/my-account/
await axios
.post("http://somewhere.com/my-account/", params(), config, {})
.then(function (response) {
console.log("request succesfully made");
})
.catch(function (error) {
// seeing the error response code
console.log(error.response.status);
})
.finally(function () {
// always executed
fs.writeFileSync("./r.txt", String(i));
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.