創(chuàng)建一個(gè)新的promise實(shí)例
在function里面調(diào)用方法后再結(jié)束時(shí)調(diào)用resolve()
觸發(fā).then()后面的事件
then()里面應(yīng)該return一個(gè)方法秕重,這樣才能控制前面的方法執(zhí)行完的前提下才執(zhí)行then里的方法赡模。
let msg = 'aaa'
function getInfo() {
setTimeout(() => {
console.log('Promise222');
console.log(msg)
}, 2000)
}
let promise = new Promise(function(resolve, reject) {
msg = 'bbb'
setTimeout(() => {
console.log('Promise111');
resolve()
}, 3000)
});
promise.then(function() { return getInfo() })
多次遍歷調(diào)用同一方法且要上一個(gè)執(zhí)行完成后才執(zhí)行下一個(gè)
// 多次遍歷調(diào)用統(tǒng)一方法時(shí)
function promise(val, await) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log(val)
resolve()
}, await)
})
}
promise('promise1', 5000).then(function() {
return promise('promise2', 2000)
}).then(function() {
return promise('promise3', 1000)
}).then(function() {
alert('finished')
})
```