Promise用法总结
new Promise(function(resolve,reject){
});
var aPromise = new Promise(function (resolve) {
resolve(100);
});
var thenPromise = aPromise.then(function (value) {
console.log(value);
});
var catchPromise = thenPromise.catch(function (error) {
console.error(error);
});
console.log(aPromise !== thenPromise); // => true
console.log(thenPromise !== catchPromise);// => true
getUserByName('xxx').then(function(user){
if(imMemoryCache[user.id]){
return inMemoryCache[user.id];
}
return getUserAccountById(user.id);
}).then(function(userAccount){
});
new Promise(function(resolve,reject){
resolve(syncValue);
}).then(/*...*/)
//可以写成
Promise.resolve(syncValue).then(/*...*/)
//一个错误也可以写成
Promise.reject(new Error('...'))
Promise.resolve('foo').then(Promise.resolve('bar')).then(function(result){
console.log(result);
});
//会被解释为:
Promise.resolve('foo').then(null).then(function(result){
console.log(result);
});
// 写法一
f1().then(function () {
return f2();
}); // 写法二
f1().then(function () {
f2();
}); // 写法三
f1().then(f2()); // 写法四
f1().then(f2);
promise1.then(function(){
return promise2;
}).then(function(){
return promise3;
}).then(function(){
return promise4;
}).catch(function(err){
console.log(err);
});
function executeSequentially(promiseFactories){
var res = Promsie.resolve();
promiseFactories.forEach(function(promiseFactory){
res = res.then(promiseFactory);
});
return res;
}
//工厂函数只是简单的创建一个promise
function promiseFactory(){
return createAPromise();
}
//原生版本
function Http(){ } Http.prototype = {
get:function(opts){
return this.ajax(opts);
},
post:function(){
return this.ajax(opts);
},
ajax:function(opts){
return Promise(function(resolve,reject){
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('...');
var {url,method,data,async,success,fail} = options;
var sendBody = null;
var qs = Object.keys(data).reduce(function(cur,pre,index){
return pre + '&' + encodeURIComponent(cur) + '=' + encodeURIComponent(data[cur]);
},'').slice(1);
if(medthod == 'get'){
url += '?' + qs;
}
else if(medhot == 'post'){
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
sendBody = qs || null;
}
xhr.onreadystatechange = function(){
if(xhr.readystate == 4){
if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
resolve(xhr,xhr.responseText);
}
}
else{
reject(xhr,xhr.responseText);
}
}
xhr.open(method,url,async);
xhr.send(sendBody);
});
}
} //jquery版本
function Http(){ } Http.prototype = {
get:function(opts){
return this.ajax(opts);
},
post:function(opts){
return this.ajax(opts);
},
ajax:function(){
return $.ajax(opts);
}
}
Promise用法总结的更多相关文章
- c/c++ 多线程 等待一次性事件 std::promise用法
多线程 等待一次性事件 std::promise用法 背景:不是很明白,不知道为了解决什么业务场景,感觉std::async可以优雅的搞定一切的一次等待性事件,为什么还有个std::promise. ...
- ES6语法 promise用法
ES6语法 promise用法 function doSomething(){ return new Promise((resolve,reject)=>{ resolve('jjjj');// ...
- es6的promise用法详解
es6的promise用法详解 promise 原理 promise是es6的异步编程解决方案, 是es6封装好的对象: 一个promise有三种状态:Pending(进行中).Resolved(已完 ...
- [WinJS] Promise 用法
初学 WinJS, 可能对 Promise 的用法不大清楚,本文简要说明一下 WinJS中 promise 的基本用法. 主要参考自:http://try.buildwinjs.com/#promis ...
- Es6 Promise 用法详解
Promise是什么?? 打印出来看看 console.dir(Promise) 这么一看就明白了,Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方 ...
- ES6 Promise 用法讲解
Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.catch等同样很眼熟的方法. 那就new一个 var p = new Promise( ...
- Promise 用法
Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.catch等同样很眼熟的方法. 那就new一个 Promise的构造函数接收一个参数,是 ...
- ES6 Promise用法讲解
所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果. ES6 规定,Promise对象是一个构造函数,用来生成Promise实例. 下面代码创造了一个 ...
- ES6 Promise 用法转载
Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.catch等同样很眼熟的方法. 那就new一个 var p = new Promise( ...
- promise用法详解
es6的promise可谓是异步书写的一大福音,过去异步js书写是函数嵌套函数的方式,promise出现后书写异步js代码就变得美观得多了 以前的写法:回调函数不停嵌套 ajax('/banners' ...
随机推荐
- InnoDB锁冲突案例演示(续)
Preface I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the ...
- 使用unittest里面的discover()方法组织测试用例
import osimport unittest directory = os.getcwd()# 测试用例的目录organize = unittest.defaultTestLoader.disco ...
- 【system.array】使用说明
对象:system.array 说明:提供一系列针对数组类型的操作 目录: 方法 返回 说明 system.array.join( array, separator ) [String] 将数组转换 ...
- Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】
Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-24 | Spring Cloud Confi ...
- visionpro9.0破解
visionpro9.0软件下载 提供一个visionpro9.0视频教程学习网站:点击下面链接进入. ------------------------Halcon,Visionpro高清视频教程,点 ...
- Halcon和visionPro的比较
很多朋友会问到visionpro和halcon这两款机器视觉软件,到底学哪个好呢,今天重码网就给大家讲一讲: 首先比较下两者的优缺点: halcon: 提供的图像算法要比Visionpro多,也就是说 ...
- CDH/Hadoop 5.15 installation steps
I will talk the main steps to install CDH 5.15 on Linux(CENT OS 6.10). The installation method is M ...
- nordic mesh中的消息缓存实现
nordic mesh中的消息缓存实现 代码文件msg_cache.h.msg_cache.c. 接口定义 头文件中定义了四个接口,供mesh协议栈调用,四个接口如下所示,接口的实现代码在msg_ca ...
- [leetcode-738-Monotone Increasing Digits]
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [leetcode-667-Beautiful Arrangement II]
Given two integers n and k, you need to construct a list which contains n different positive integer ...