Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
一组小清新的promise风格小函数
function get(uri) {
return http(uri,'GET');
}
function post(uri,data) {
if(typeof data === 'object' && !(data instanceof String || (FormData && data instanceof FormData))) {
var params = [];
for(var p in data) {
if(data[p] instanceof Array) {
for(var i = 0; i < data[p].length; i++) {
params.push( encodeURIComponenet(p) + '[]=' + encodeURIComponenet(data[p][i]);
}
} else {
params.push( encodeURIComponenet(p) + '=' + encodeURIComponenet(data[p]);
}
}
data = params.join('&');
}
return http(uri,'POST',data);
}
function http(uri,method,data) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(method,uri,true);
xhr.send(data);
xhr.addEventListener('readystatechange',function(e){
if(xhr.readyState === 4) {
if(xhr.status === 200) {
resolve(xhr.responseText);
} else {
reject(xhr);
}
}
})
})
}
function wait(duration) {
return new Promise(function(resolve, reject) {
setTimeout(resolve,duration);
})
}
function waitFor(element, event, useCapture){
return new Promise(function(resolve, reject) {
element.addEventListener(event, function listener(event){
resolve(event)
this.removeEventListener(event, listener, useCapture);
},useCapture)
})
}
function loadImage(src) {
return new Promise(function(resolve, reject) {
var image = new Image;
image.src = src;
image.addEventListener('load',function() {
resolve(image);
});
image.addEventListener('error',reject);
})
}
function runScript(src) {
return new Promise(function(resolve, reject) {
var script = document.createElement('script');
script.src = src;
script.addEventListener('load',resolve);
script.addEventListener('error',reject);
(document.getElementsByTagName('head')[0] || document.body || document.documentElement).appendChild(script);
})
}
@ziwenhan

This comment has been minimized.

Copy link

@ziwenhan ziwenhan commented Jan 13, 2015

有些 function 后面的花括号没有空格……好想空格

@wintercn

This comment has been minimized.

Copy link
Owner Author

@wintercn wintercn commented Jan 13, 2015

@VinnSung 满足你了

@myst729

This comment has been minimized.

Copy link

@myst729 myst729 commented Jan 13, 2015

为啥不混一些VS风格的函数大括号?

@hax

This comment has been minimized.

Copy link

@hax hax commented Jan 13, 2015

post 没有 setRequestHeader content-type 嘛,差评!
还有 response code 2xx 应该都是 resolve 滴。

@hax

This comment has been minimized.

Copy link

@hax hax commented Jan 13, 2015

waitFor 后没有 remove listener。。。

@tangyangzhe

This comment has been minimized.

Copy link

@tangyangzhe tangyangzhe commented Jan 14, 2015

encodeURIComponenet 拼错了吧

@TooBug

This comment has been minimized.

Copy link

@TooBug TooBug commented Mar 18, 2015

有些参数分隔逗号后面没有空格……好想空格……

@Justineo

This comment has been minimized.

Copy link

@Justineo Justineo commented Mar 18, 2015

咋办,上面吐槽的都差不多了……

@LiuJi-Jim

This comment has been minimized.

Copy link

@LiuJi-Jim LiuJi-Jim commented Mar 18, 2015

304不兼容一发?

@Rayi

This comment has been minimized.

Copy link

@Rayi Rayi commented Mar 18, 2015

winter竟然是男的! 这个吐槽如何!

@stableShip

This comment has been minimized.

Copy link

@stableShip stableShip commented Mar 18, 2015

学习

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment