Promise是ES6引入的异步编程的新解决方案,语法上promise是一个构造函数用来封装异步操作病可以获取其成功或失败的结果。
1.Promise构造函数
:Primise(){ }
2.Promise.prototype.then
方法
3.Promise.prototype.catch
方法
//实例化promise对象
const p=new Promise(function(resolve,reject){
setTimeout(()=>{
let data='数据'
//调用resolve
// resolve(data)
reject('iiii')
},1000)
})
p.then(function(value){
console.log(value);//这里打印resolve传过来的数据
},function(resolve){
console.log(resolve);//这里打印reject传过来的数据
})
案例1:使用nodejs的fs模块读取文件
新建一个demo.js文件,写入以下内容:
// 1.引入node的fs模块
const fs = require('fs');
// 2.调用方法读取文件
fs.readFile('./test.md', (err, data) => {
//如果失败,则抛出错误
if (err) throw err;
console.log(data.toString()); //这里打印的是一个buffer对象,我们可以调用toString方法,将其转换为字符串
})
在终端中执行:node demo.js
即可执行当前文件.
案例2:使用Promise封装案例1
// 1.引入node的fs模块
const fs = require('fs');
// 2.使用Promise封装
const p = new Promise(function (resolve, reject) {
//这里执行异步操作
fs.readFile('./test.md', (err, data) => {
//如果读取失败则调用reject返回错误信息
if (err) reject(err);
//如果读取成功,则调用resolve方法
resolve(data)
})
})
p.then(function (value) {
console.log(value.toString());
}, function (reason) {
console.log(reason);
})
日常开发中,当我们需要操作多个文件时,就可以通过Promise对其进行封装,如上所示.
案例2:使用Promise封装AJAX
//不使用Promise封装
// 1.创建xml对象
const xhr=new XMLHttpRequest();
// 2.初始化
xhr.open('GET','http://localhost:8000/students');
// 3.发送
xhr.send();
// 4.绑定事件,处理响应结果
xhr.onreadystatechange=function(){
//判断
if(xhr.readyState===4){
//判断响应状态码200-299 都表示成功
if(xhr.status>=200&&xhr.status<300){
//表示成功
console.log(xhr.response);
}else{
console.error(xhr.status)
}
}
}
//使用Promise封装
//直接在外层包装promise
let p=new Promise((resolve,reject)=>{
// 1.创建xml对象
const xhr=new XMLHttpRequest();
// 2.初始化
xhr.open('GET','http://localhost:8000/students');
// 3.发送
xhr.send();
// 4.绑定事件,处理响应结果
xhr.onreadystatechange=function(){
//判断
if(xhr.readyState===4){
//判断响应状态码200-299 都表示成功
if(xhr.status>=200&&xhr.status<300){
//表示成功
resolve(xhr.response)
}else{
reject(xhr.status)
}
}
}
})
//指定回调函数
p.then(function(value){
console.log(value);
},function(reason){
console.log(reason);
})
案例3:Promise封装,读取多个文件
// fs读取多个文件(不适用Promise封装)
const fs = require('fs');
fs.readFile('./test1.md', (err1, data1) => {
fs.readFile('./test2.md', (err2, data2) => {
fs.readFile('./test3.md', (err3, data3) => {
let re = data1 + '\r\n' + data2 + '\r\n' + data3;
console.log(re);
})
})
})
//使用Promise封装
const fs = require('fs');
const p = new Promise((resolve, reject) => {
fs.readFile('./test1.md', (err, data) => {
resolve(data) //*
})
})
p.then(value => { //现在的value是接受到的*行传递过来的数据
return new Promise((resolve, reject) => {
fs.readFile('./test2.md', (err, data) => {
resolve([value, data]) //将读取到的test1.md文件内容和test2.md文件内容放在一个数组中返回
})
})
}).then(value => { //此时的value为test1.md文件和test2.md两个文件的内容
return new Promise((resolve, reject) => {
fs.readFile('./test3.md', (err, data) => {
resolve(value.concat(data))
})
})
}).then(value => {
console.log(value.join('\r\n'));
})
Promise.catch
方法
Promise.catch方法用来制定promise失败的回调
//promise.catch方法
const p = new Promise((resolve, reject) => {
setTimeout(() => {
//设置promise对象的状态为失败,并设置失败的值
reject('出错啦!')
}, 1000)
})
// 方法1
// p.then(回调1,回调2):回调1是在promise内部调用了resolve之后执行的回调函数,
// 回调2是在promise内部调用了reject之后执行的回调
// p.then(function (value) {}, function (reason) {
// console.log(reason);
// })
// 方法2:直接调用promise.catch方法
p.catch(reason => {
console.log(reason);
})
promise.catch方法其实是promise.then方法的语法糖,它省略了对成功状态的处理回调,
这样写更简洁,单独使用promise.then也能实现效果
网友评论