-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
62 lines (47 loc) · 1023 Bytes
/
browser.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
// for browser
function getImg(url) {
var p = new Promise();
var img = new Image();
img.onload = function() {
p.resolve(this);
}
img.onerror = function(err) {
p.reject(err);
}
img.src = url;
return p;
};
function upload(params) {
var p = new Promise();
var xhr = new XMLHttpRequest();
var fd = new FormData();
fd.append('data', params.data);
xhr.onload = function(res) {
p.resolve(res);
}
xhr.onabort = function() {
p.reject(new Error('abort'));
};
xhr.onerror = function(err) {
p.reject(err);
};
xhr.open('POST', params.url);
xhr.send(fd);
return p;
};
function getData(json) {
return Promise(function(resolve) {
$.getJSON(json, function(data) {
resolve(data);
});
});
};
function sleep(ms) {
return function(v) {
var p = Promise();
setTimeout(function() {
p.resolve(v);
}, ms);
return p;
};
};