es5 中
var obj = {
ajax: function (callback) {
console.log('执行')
setTimeout(function () {
callback && callback.call()
}, 1000)
}
}
export default obj
import promise from '@/utils/promise.js'
promise.ajax(function () {
console.log('timeout1')
})
es6
var obj = {
ajax: function (callback) {
console.log('执行')
setTimeout(function () {
callback && callback.call()
}, 1000)
},
pajax: function () {
console.log('执行2')
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve()
}, 1000)
})
}
}
export default obj
/**
* 异步 promise
*/
// https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/0014345008539155e93fc16046d4bb7854943814c4f9dc2000
// job1.then(job2).then(job3).catch(handleError);
// 其中,job1、job2和job3都是Promise对象。
// resolve()执行的是后面的then里面的成功的函数 理解resolve then
promise.pajax().then(function () {
console.log('timeout2')
})
promise.all