同步异步问题,找的练习题

107 阅读1分钟

【第1题】:

1】、 async function async1() {
            console.log('asyncl start');
            await async2();
            console.log('asyncl end');
        }

        async function async2() {
            console.log('async2 start');
            return new Promise((resolve, reject) => {
                resolve();
                console.log('async2 promise');
            });
        }

        console.log('script start'); 

        setTimeout(function () {
            console.log('setTimeout');
        }, 0);

        async1();

        new Promise(function (resolve, reject) {
            console.log('promisel');
            resolve();
        }).then(function () {
            console.log('promise2');
        }).then(function () {
            console.log('promise3');
        });

        console.log('script end');

1、答案:
script start
asyncl start
async2 start
async2 promise
promisel
script end
promise2
promise3
asyncl end
setTimeout

分析:s1.plumeta.com/i/2022/09/0…


【第2题】:
2】、 async function async1() {
            console.log("async1 start");
	    await async2();
	    console.log("async1 end");
	}
	async function async2() {
	    console.log("async2");
	}
	console.log("script start");
	setTimeout(function () {
	    console.log("setTimeout");
	}, 0);
	async1();
	new Promise(function (resolve) {
	    console.log("promise1");
	    resolve();
	}).then(function () {
	    console.log("promise2");
	});
	console.log("script end");

2、答案:
script start
async1 start
async2
promise1
script end
async1 end
promise2
setTimeout


【第3题】:
3】、 setTimeout(function () {
            console.log(1)
        }, 0);
        new Promise(function (resolve, reject) {
            console.log(2)
            for (var i = 0; i < 10000; i++) {
                if (i === 10) { 
                  console.log(10) 
                }
                i == 9999 && reject();
            }
            console.log(3)
        }).then(function () {
            console.log(4)
        })
        console.log(5);	

3、答案:2、10、3、5、1


【第4题】
4】、setTimeout(_ => console.log(4));
        new Promise(resolve => { 
            resolve();
            console.log(1)
        }).then(_ => { 
            console.log(3);
            Promise.resolve().then(_ => { 
                console.log('beforeTimeout')
            }).then(_ => { 
                Promise.resolve().then(_ => { 
                    console.log('alsoBeforeTimeout')
                }) 
            }) 
        });
        console.log(2);

4、答案:1、2、3、beforeTimeout、alsoBeforeTimeout、4


【第5题】
5】、 const promise = new Promise((resolve, reject) => {
            console.log(1);
            setTimeout(() => {
                console.log('timerStart');
                resolve('success');
                console.log('timerEnd');
            }, 0)
            console.log(2);
        })
        promise.then((res) => {console.log(res);})
        console.log(4);

5、答案:1、2、4、timerStart、timerEnd、success


【第6题】
【6】、 const a = new Promise(function(resolve, reject){
            reject();
        });
        a.then( res => {
            console.log(1);
        }).catch( err => {
            console.log(2);
        }).then( res => {
            console.log(3);
        }).catch( err => {
            console.log(4);
        }).then( res => {
            console.log(5);
        }).catch( err => {
            console.log(6);
        })

6、答案:2、3、5


【第7题】
7】、 console.log('script start');
        setTimeout(function() {
           new Promise(resolve=>{
                console.log('000');
                resolve();
            }).then(res=>{
                console.log('这是微任务');
            })
            console.log('timeout1');
        }, 10);

        new Promise(resolve => {
            console.log('promise1');
            resolve();
            setTimeout(() => console.log('timeout2'), 10);
        }).then(function() {
            console.log('then1')
        }).then(function() {
            console.log('then2')
        })
        console.log('script end');

7、答案:
script start
promise1
script end
then1
then2
000
timeout1、
这是微任务
timeout2


【第8题】
8】、 async function async1() {
            console.log('async1 start')
            await async2();
            console.log('async1 end');
        }

        async function async2() {
            console.log('async2 start');
            return new Promise((resolve, reject) => {
                resolve();
                console.log('async2 promise');
            })
        }

        console.log('script start');

        setTimeout(function () {
            console.log('setTimeout');
        }, 0)

        async1();

        new Promise(function (resolve) {
            console.log('promise1');
            resolve();
        }).then(function () {
            console.log('promise2');
        }).then(function () {
            console.log('promise3');
        })

        console.log('script end');

8、答案:
script start
async1 start
async2 start
async2 promise
promise1
script end
promise2
promise3
async1 end
setTimeout