DDGarfield 2022-06-23 19:02:41 阅读数:779
javascript in ,
promise
yes ES6
Chinese language standard , Save an event that will end in the future ( This is usually an asynchronous operation ) Result .const promise = new Promise(function(resolve, reject) {
// ... some code
if (/* Asynchronous operation succeeded */){
resolve(value);
} else {
reject(error);
}
});
In the work , There is a business function : Periodic scan task , Each cycle will scan out data , Then save it to... According to the number of cycles elasticsearch
, Now it is necessary to check whether the data between each cycle has overlapping and related data to support Periodic snapshot function , adopt Query DSL +kibana
Visual query , Or it can be compared with the naked eye , It's hard work .kibana
through http post
request , Then return the data json
, Then we can simulate kibana
request , get data , Then compare the adjacent period data through the code , The output file , The idea of a small crawler and a small tool for data analysis came to mind .
Because the real code is in the company , The sample code has been modified ,url
Replaced by Baidu ,post
Replace with get
const http = require('https');
function spider() {
for (i = 0; i < 9; i++) {
//i For a period parameter
http.get('https://www.baidu.com/', function (res) {
console.log(` At present i:${i}`);
console.log(` Status code ${res.statusCode}`);
})
}
}
spider()
node .\src\server\index.js
At present i:9
Status code 200
At present i:9
Status code 200
At present i:9
Status code 200
At present i:9
Status code 200
At present i:9
Status code 200
At present i:9
Status code 200
At present i:9
Status code 200
At present i:9
Status code 200
At present i:9
Status code 200
Why is that ? because http.get
It's an asynchronous method , Not waiting , The loop will continue ,i
The value will also change , In this case, the asynchronous method is used to i
The reference of becomes 9. We definitely want to output something different i
value , To view the data of the corresponding period . What do I do ? Anonymous functions
const http = require('https');
function spider() {
for (i = 0; i < 9; i++) {
// Closure
// Anonymous functions + Execute now
(function (i) {
http.get('https://www.baidu.com/', function (res) {
console.log(` At present i:${i}`);
console.log(` Status code ${res.statusCode}`);
})
})(i)
}
}
spider()
node .\src\server\index.js
At present i:1
Status code 200
At present i:6
Status code 200
At present i:5
Status code 200
At present i:2
Status code 200
At present i:4
Status code 200
At present i:3
Status code 200
At present i:0
Status code 200
At present i:8
Status code 200
At present i:7
Status code 200
Closure = function + Citation environment , Functions are anonymous functions , The reference environment is the parameter i
value
If the requirement is to view the cycle : Cycle data , such key:value
The needs of , So the above requirements have been basically met , If you think anonymous functions + Immediate execution is not easy to understand , The transformation is as follows , Also understand .
const http = require('https');
//let map=new Map();
function getData(i) {
http.get('https://www.baidu.com/', function (res) {
console.log(` At present i:${i}`);
console.log(res.statusCode);
//map.set ...
})
}
function spider() {
for (i = 0; i < 9; i++) {
getData(i)
}
}
spider()
But now , We need to save every cycle and cycle data , Then do data analysis . In other words , We need to create multiple loops http Asynchronous requests , All done , And return data , Coexist , To do analysis . Don't forget that this is asynchronous , Define global variables let map=new Map();
And in asynchronous callbacks map.set
This will not work . This is the time. Promise
On the stage .
const http = require('https');
// Define an array
let array = Array();
// Define an array
let promiseArray = []
// Define a map
let map=new Map();
function getData(i) {
return new Promise((resolve,reject) => {
http.get('https://www.baidu.com/', function (res) {
console.log(` At present i:${i}`);
console.log(res.statusCode);
array.push(i);
map.set(i,res.statusCode);
resolve();
})
})
}
function spider() {
console.log(' Start cycle creation promiseArray');
for (i = 0; i < 9; i++) {
promiseArray.push(getData(i))
}
console.log(' End cycle creation promiseArray');
}
spider();
//Promise.all(iterable) Method returns a Promise example , This example is in iterable All in parameters promise all “ complete (resolved)” Or parameter does not contain promise When the callback completes (resolve); If the parameter promise There is a failure (rejected), This instance callback failed (reject), The reason for failure is the first failure promise Result .
Promise.all(promiseArray).then(function(){
console.log(array.length);
console.log(array);
console.log(map);
})
Each asynchronous request creates a Promise
object , And put it into a storage Promise
An array of objects , And then call Promise.all
, Or return one Promise
object , His callback completion is Promise
Each of the objects in the array is resolve
, That is, all asynchronous requests have been completed .
Start cycle creation promiseArray
End cycle creation promiseArray
At present i:7
200
At present i:4
200
At present i:1
200
At present i:2
200
At present i:6
200
At present i:5
200
At present i:8
200
At present i:3
200
At present i:0
200
9
[
7, 4, 1, 2, 6,
5, 8, 3, 0
]
Map {
7 => 200,
4 => 200,
1 => 200,
2 => 200,
6 => 200,
5 => 200,
8 => 200,
3 => 200,
0 => 200
}
版权声明:本文为[DDGarfield]所创,转载请带上原文链接,感谢。 https://qdmana.com/2022/174/202206231758267267.html