function asyncFun(a,b,cb) { setTimeout(function(){ cb(a+b) },200) } asyncFun(1,2,function(result){ console.log(result) }) console.log(2) // 先返回2再返回3
// promise 产生的原因 function asyncFun(a,b,cb) { setTimeout(function(){ cb(a+b) },200) } asyncFun(1,2,function(result){ if (result>2) { asyncFun(result,2,function(result){ if (result>4) { asyncFun(result,2,function(result){ if (result>4) { console.log('ok') } }) } }) } }) console.log(2)//2之后才是ok
function asyncFun(a,b) { return new Promise(function(resolve,reject){ if (typeof a!=='number'||typeof b!=='number') { reject(new Error('no number')) } setTimeout(function(){ resolve(a+b) },200) }) } asyncFun(1,'a') .then(function(result){ if (result>2) { return asyncFun(result,2) } }).then(function(result){ if (result>4) { console.log('ok') } }).catch(function(error){ console.log('second'+error) })//secondError: no number
class User{ constructor(name,password){ this.name = name; this.password = password } validateName(){ let name = this.name return new Promise(function(resolve,reject){ setTimeout(function(){ if (name === 'Leo') { resolve('success') }else{ reject('error') } }) }) } validatePwd(){ let password = this.password return new Promise(function(resolve,reject){ setTimeout(function(){ if (password === '123') { resolve('success') }else{ reject('error') } }) }) } } const user = new User('Leo','123'); user.validateName().then(function(result){ return user.validatePwd() }).then(function(result){ console.log('登录成功') }).catch(function(err){ console.log("err") }) // 登录成功
class User{ constructor(name,password){ this.name = name; this.password = password } validateName(){ let name = this.name return new Promise(function(resolve,reject){ setTimeout(function(){ if (name === 'Leo') { resolve('success') }else{ reject('error') } }) }) } validatePwd(){ let password = this.password return new Promise(function(resolve,reject){ setTimeout(function(){ if (password === '123') { resolve('success') }else{ reject('error') } }) }) } } const user = new User('Leo','123'); user.validateName().then(function(result){ return 'Ok' }) // 如果不是promise的话会自己封装成下面这种promise方法 // Promise.resolve('ok') // var promise = new Promise(function(resolve,reject){ // resolve('ok') // }) .then(function(result){ console.log(result) }) // OK
class User{ constructor(name,password){ this.name = name; this.password = password } validateName(){ let name = this.name return new Promise(function(resolve,reject){ setTimeout(function(){ if (name === 'Leo') { resolve('success') }else{ reject('error') } }) }) } validatePwd(){ let password = this.password return new Promise(function(resolve,reject){ setTimeout(function(){ if (password === '123') { resolve('success') }else{ reject('error') } }) }) } } // const user = new User('Leo1','123'); // user.validateName().then(function(result){ // throw new Error('firsterr') // return user.validatePwd() // },function(err){ // console.log('seconderr')//这是出现错误所调用的 // }).catch(function(err){ // console.log(err) // }) // // seconderr // const user = new User('Leo','123'); // user.validateName().then(function(result){ // throw new Error('firsterr') // return user.validatePwd() // },function(err){ // console.log('seconderr')//这是出现错误所调用的 // }).catch(function(err){ // console.log(err) // }) // firsterr // const user = new User('Leo','1234'); // user.validateName().then(function(result){ // throw new Error('firsterr') // return user.validatePwd() // },function(err){ // console.log('seconderr')//这是出现错误所调用的 // }).catch(function(err){ // console.log(err) // return user.validatePwd() // }).then(function(result){ // console.log(result) // }) // 密码正确的话就显示firsterr 不正确的话显示 firsterr error const user = new User('Leo','1234'); user.validateName().then(function(result){ throw new Error('firsterr') return user.validatePwd() },function(err){ console.log('seconderr')//这是出现错误所调用的 }).catch(function(err){ console.log(err) return user.validatePwd() }).then(function(result){ console.log(result) }).catch(function(err){ })
function asyncFun(a,b) { return new Promise(function(resolve,reject){ if (typeof a!=='number'||typeof b!=='number') { reject(new Error('no number')) } setTimeout(function(){ resolve(a+b) },200) }) } // 只要一个有错误就不会执行promise // var promise = Promise.all([asyncFun(1,2),asyncFun(3,4),asyncFun(5,6)]); // promise.then(function(result){ // console.log(result)//[ 3, 7, 11 ] // }) // var promise = Promise.race([asyncFun(1,2),asyncFun(3,4),asyncFun(5,6)]); // promise.then(function(result){ // console.log(result)//[ 3 ] // }) // var promise = Promise.race([asyncFun(1,'2'),asyncFun(3,4),asyncFun(5,6)]); // promise.then(function(result){ // console.log(result)//[ 3 ] // }) // no number