Vue.js 学习笔记十四:Promise 之三种状态

目录

Promise 三种状态


Promise 三种状态

  • 初始化状态(等待状态):pending

  • 成功状态:fullfilled

  • 失败状态:rejected

     

new Promise() 执行之后,promise 对象的状态会被初始化为pending,这个状态是初始化状态。new Promise()这行代码,括号里的内容是同步执行的。括号里定义一个 function,function 有两个参数:resolve 和 reject。如下:

  • 如果请求成功了,则执行 resolve(),此时,promise 的状态会被自动修改为 fullfilled。

  • 如果请求失败了,则执行 reject(),此时,promise 的状态会被自动修改为 rejected。

promise.then() 方法,括号里面有两个参数,分别代表两个函数 function1 和 function2:

  • 如果 promise 的状态为 fullfilled(意思是:如果请求成功),则执行function1里的内容

  • 如果promise 的状态为 rejected(意思是,如果请求失败),则执行function2里的内容

另外,resolve()reject() 这两个方法,是可以给 promise.then() 传递参数的。

		<script type="text/javascript">
			const runAsync = () => {
				let promise = new Promise((resolve, reject) => { 
                    	// 进来之后,状态为 pending
						// 这行代码是同步的
						console.log('开始请求');
						// 开始执行异步操作(比如 ajax 请求接口,这里暂时用定时器代替)
						setTimeout(() => {
							// 接口返回的数据
							const data = {code: 200,msg: 'hello world'}
							if (data.code === 200) {
								// 如果请求成功了,请写 resolve(),此时,promise 的状态会被自动修改为fullfilled
								resolve(data)
							} else {
								// 如果请求失败了,请写 reject(),此时,promise 的状态会被自动修改为rejected
								reject({code: 500,msg: 'network error'})
							}
						}, 1000)
					})
					//调用 promise 的 then()
					.then(succ => {
							//如果 promise 的状态为 fullfilled,则执行这里的代码
							console.log(succ)
							document.getElementById('res-data').innerHTML = JSON.stringify(succ)
						},
						fail => {
							//如果 promise 的状态为 rejected,则执行这里的代码
							console.error(fail)
							document.getElementById('res-data').innerHTML = JSON.stringify(fail)

						})
				return promise
			}
		</script>

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
index.vue:201 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'type') at _callee$ (index.vue:201:1) at tryCatch (regeneratorRuntime.js:44:1) at Generator.eval (regeneratorRuntime.js:125:1) at Generator.eval [as next] (regeneratorRuntime.js:69:1) at asyncGeneratorStep (asyncToGenerator.js:3:1) at _next (asyncToGenerator.js:22:1) at eval (asyncToGenerator.js:27:1) at new Promise (<anonymous>) at eval (asyncToGenerator.js:19:1) at VueComponent.handleNodeClick (index.vue:227:1) _callee$ @ index.vue:201 tryCatch @ regeneratorRuntime.js:44 eval @ regeneratorRuntime.js:125 eval @ regeneratorRuntime.js:69 asyncGeneratorStep @ asyncToGenerator.js:3 _next @ asyncToGenerator.js:22 eval @ asyncToGenerator.js:27 eval @ asyncToGenerator.js:19 handleNodeClick @ index.vue:227 handleCurrentChange @ index.vue:197 invokeWithErrorHandling @ vue.runtime.esm.js:3971 invoker @ vue.runtime.esm.js:1188 invokeWithErrorHandling @ vue.runtime.esm.js:3971 Vue.$emit @ vue.runtime.esm.js:2874 eval @ element-ui.common.js:1116 eval @ vue.runtime.esm.js:4097 flushCallbacks @ vue.runtime.esm.js:4019 Promise.then(异步) timerFunc @ vue.runtime.esm.js:4044 nextTick @ vue.runtime.esm.js:4109 queueWatcher @ vue.runtime.esm.js:3346 Watcher.update @ vue.runtime.esm.js:3584 Dep.notify @ vue.runtime.esm.js:710 reactiveSetter @ vue.runtime.esm.js:4380 proxySetter @ vue.runtime.esm.js:5158 handleCurrentChange @ element-ui.common.js:1069 invokeWithErrorHandling @ vue.runtime.esm.js:3971 invoker @ vue.runtime.esm.js:1188 invokeWithErrorHandling @ vue.runtime.esm.js:3971 Vue.$emit @ vue.runtime.esm.js:2874 onPagerClick @ element-ui.common.js:547 invokeWithErrorHandling @ vue.runtime.esm.js:3971 invoker @ vue.runtime.esm.js:1188 original_1._wrapper @ vue.runtime.esm.js:7265
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

stary1993

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值