为什么这段 Promise.resolve 的 then 不继续执行
发布于 7 年前 作者 think2011 6670 次浏览 来自 问答

直接上代码:

var p = Promise.resolve({
	then: x => {
		console.log('ok')
	}
})

p.then(x=> { console.log('为什么这里不继续执行了?') })
3 回复
var p = Promise.resolve()
.then(x=> { console.log('ok') })
.then(x=> { console.log('这样就能') })

var p = new Promise(resolve => { resolve() })
.then(x=> { console.log('ok') })
.then(x=> { console.log('这样也能') })
var p = Promise.resolve({ // 这个是一个 thenable 对象,
	then: x => { // 这里 then 接受两个参数 resolve, 和 reject,有点像传进 new Promise 里的函数的参数,也就是说这里需要手动调用 resolve 或者 reject,p 的 then 里的函数才会被调用
		console.log('ok');
		x(); // 也就是说这里需要调用下 x 函数
	}
})

@zbinlin 理解了,非常感谢!

回到顶部