前端异步流程工具
- Promise
const p1 = new Promise ( function ( resolve, reject ) {
resolve( '任务一' )
})
.then ( function ( data ) {
console.log( data )
const p2 = new Promise ( function ( resolve, reject ) {
setTimeout( function () {
resolve( '任务二' )
},1000)
})
.then ( function ( data ) {
console.log( data )
})
console.log( '主线程任务 ')
//主线程任务 任务一 任务二
Promise.all()中参数为数组,即all中的执行完毕后再执行后面的
Promise.race()中参数为数组,谁快谁想输出
- Generator函数
- 在function 关键字后面写一个* ,表示一个generator函数
- generator通过 yield 关键字来定义任务
function* p1(){
yield '任务1';
yield '任务2';
yield function* p2() {
yield '任务3'
};
return '任务4'
}
var p = p1()
console.log(p.next())
console.log(p.next())
console.log(p.next())
co