-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
76 lines (63 loc) · 1.7 KB
/
promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var path = require('path');
function asyncFunction(i) {
// return new Promise((resolve, reject) => {
// resolve(i);
// // reject(i);
// });
return Promise.resolve(i);
}
// asyncFunction().then(res => {
// console.log(res);
// }).catch(err =>{
// console.log(err);
// });
for (var promiseArr = [], i = 0; i < 12; i++) {
promiseArr.push(asyncFunction(i));
}
Promise.all(promiseArr).then(res => {
console.log(res);
}).catch(function onRejected(err) {
console.error('error', err);
});
// function ajax(url) {
// return function(){
// var req = new XMLHttpRequest();
// req.open('GET', url, true);
// req.onload = ()=>{
// Promise.resolve(req.responseText)
// }
// req.send();
// }
// }
function getURL(url) {
return new Promise((resolve, reject) => {
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onload = () => {
if (req.status == 200) {
resolve(req.responseText);
} else {
reject(new Error(req.statusText));
}
};
req.onerror = () => {
reject(new Error(req.statusText));
};
req.send();
});
}
var URL = "http://httpbin.org/get";
getURL(URL).then(function onFulfilled(res) {
console.log('66666');
console.log(res);
}).catch(function onRejected(err) {
console.log('error');
console.error(err);
});
console.log('====================');
console.log(__dirname, __filename);
var demo = require('./1');
console.log(demo);
console.log(module);
console.log('====================');