You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'use strict'classUser{constructor(name,password){this.name=namethis.password=password}validateName(cb){letname=this.namereturnnewPromise((resolve,reject)=>{setTimeout(()=>{//模拟异步if(name==='leo'){resolve('success')}else{reject('error')}},500)})}validatePassword(cb){letpassword=this.passwordreturnnewPromise((resolve,reject)=>{setTimeout(()=>{//模拟异步if(password==='123'){resolve('success')}else{reject('error')}},500)})}}constuser=newUser('leo','123')user.validateName().then((result)=>{//如果名称验证通过,标示执行成功,则执行本函数,并接受参数'success'console.log(result)//可以直接返回一个非Promisereturn'name ok'//会自动封装成 Promise.resolve('name ok') 返回,供下一个then调用}).then((result)=>{//接受上一个的返回值console.log(result)}).catch((error)=>{// 任意一个参数检测不通过,执行reject,本函数,接受参数'error'console.log(error)})// success// name ok
The text was updated successfully, but these errors were encountered: