console
const fs = require('fs')
const path = require('path')
//1. 这是普通读取文件的方法
/* fs.readFile(path.join(__dirname,'111.txt'),'utf-8',(err,data) => {
if(err) throw err
console.log(data)
}) */
//2. 用回调函数分别放成功和失败两个结果 缺点:两个结果都放在一个函数里面
/* function getFilePath(path,callback){
fs.readFile(path,'utf-8',(err,data) => {
if(err) return callback(err)
callback(null,data)
})
}
getFilePath('./111.txt', function(err,data){
if(err) return console.log(err)
console.log(data)
}) */
//3. 分别用两个函数存放成功失败两个结果 接近于promise的用法
/* function getFilePath(path,errcb,succcb){
fs.readFile(path,'utf-8',(err,data) => {
if(err) return errcb(err)
succcb(data)
})
}
getFilePath('./111.txt', function(err){
console.log(err)
},function(data){
console.log(data)
} )
*/
//4
function getFilePath(path,succcb,errcb){
fs.readFile(path,'utf-8',(err,data) => {
if(err) return errcb(err)
succcb(data)
})
}
getFilePath('./111.txt', function(data){
console.log(data)
getFilePath('./222.txt', function(data){
console.log(data)
})
})
promise
promise 使用 :
var promise = new Promise(function(){
})
const fs = require('fs')
const path = require('path')
// 特点:创建即调用 ,不符合使用需求
/*
var promise = new Promise(function(){
fs.readFile('./111.txt','utf-8',(err,data) => {
if(err) throw err
console.log(data)
})
}) */
// 创建一个函数包裹超来, 可实现按需调用执行
/* function getFileByPath(path){
var promise = new Promise( function(){
fs.readFile('./111.txt','utf-8',(err,data) => {
if(err) throw err
console.log(data)
}) //这里只需出一个结果,返回给上级, 无法判断返回结果是 成功 或 失败,看下一例解决方案
})
}
getFileByPath('./111.txt')
*/
//
/* function getFileByPath(path){
var promise = new Promise( function(resolve,reject){
fs.readFile('./111.txt','utf-8',(err,data) => {
if(err) return reject(err)
resolve(data)
})
})
return promise
}
var p = getFileByPath('./111.txt')
//p.then(function(data){},function(err){}) //这里两个function可理解为 对应用 function(resolve,reject)
p.then(function(data){
console.log(data)
},function(err){
concat.log(err.message)
})
getFileByPath('./111.txt').then(
function(data){
console.log(data)
},function(err){
concat.log(err.message)
})
*/
//
function getFileByPath(path){
var promise = new Promise( function(resolve,reject){
fs.readFile('./111.txt','utf-8',(err,data) => {
if(err) return reject(err)
resolve(data)
})
})
return promise
}
getFileByPath('./111.txt')
.then(function(data){
console.log(data)
return getFileByPath('./222.txt')
})
.then(function(data){
console.log(data)
})
// 处理错误 解决办法
unction getFileByPath(path){
var p = new Promise( function(resolve,reject){
fs.readFile(path,'utf-8',(err,datastr) => {
if(err) return reject(err)
resolve(datastr)
})
})
return p
}
getFileByPath('./11.txt')
.then(function(data){
console.log(data)
return getFileByPath('./222.txt')
},/* function(err){ //这是为了防止前面的出错不响影后面的执行
console.log('err'+err.message)
return getFileByPath('./222.txt')
}*/)
.then(function(data){
console.log(data)
})
.catch(function(err){ //用catch 会处理.then任一节点出错,不同的是,不会执行下面的代码
console.log(err.message)
})
console.log('okok')
//jquery中的 ajax 使用promiser 指定成功回调函数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="./node_modules/jquery/dist/jquery.min.js"> </script>
<script src=./a.js></script>
</head>
<body>
<input id="btn" value="获取数据" type="button" />
<script>
$(function(){
$('#btn').on('click',function(){
$.ajax({
url:'./111.txt',
type:'get',
dataType:'db.json',
/* suecces:function(){ //普通ajax的用法
console.log(data)
} */
})
.then(function(data){ // promise的用法
console.log(data)
})
})
})
</script>
</body>
</html>