密码:answer

一、异步 && 事件循环

1、 阅读代码

  1. const promise = new Promise((resolve, reject) => {
  2. console.log(1);
  3. console.log(2);
  4. });
  5. promise.then(() => {
  6. console.log(3);
  7. });
  8. console.log(4);

2、阅读代码

  1. const promise1 = new Promise((resolve, reject) => {
  2. console.log('promise1')
  3. resolve('resolve1')
  4. })
  5. const promise2 = promise1.then(res => {
  6. console.log(res)
  7. })
  8. console.log('1', promise1);
  9. console.log('2', promise2);

3、 阅读代码

  1. Promise.resolve().then(() => {
  2. console.log('promise1');
  3. const timer2 = setTimeout(() => {
  4. console.log('timer2')
  5. }, 0)
  6. });
  7. const timer1 = setTimeout(() => {
  8. console.log('timer1')
  9. Promise.resolve().then(() => {
  10. console.log('promise2')
  11. })
  12. }, 0)
  13. console.log('start');

4、 阅读代码

  1. const promise = new Promise((resolve, reject) => {
  2. console.log(1);
  3. setTimeout(() => {
  4. console.log("timerStart");
  5. resolve("success");
  6. console.log("timerEnd");
  7. }, 0);
  8. console.log(2);
  9. });
  10. promise.then((res) => {
  11. console.log(res);
  12. });
  13. console.log(4);

5、 阅读代码

  1. const promise = new Promise((resolve, reject) => {
  2. resolve('success1');
  3. reject('error');
  4. resolve('success2');
  5. });
  6. promise.then((res) => {
  7. console.log('then:', res);
  8. }).catch((err) => {
  9. console.log('catch:', err);
  10. })

6、 阅读代码

  1. Promise.resolve(1)
  2. .then(2)
  3. .then(Promise.resolve(3))
  4. .then(console.log)

7、 阅读代码

  1. const promise1 = new Promise((resolve, reject) => {
  2. setTimeout(() => {
  3. resolve('success')
  4. }, 1000)
  5. })
  6. const promise2 = promise1.then(() => {
  7. throw new Error('error!!!')
  8. })
  9. console.log('promise1', promise1)
  10. console.log('promise2', promise2)
  11. setTimeout(() => {
  12. console.log('promise1', promise1)
  13. console.log('promise2', promise2)
  14. }, 2000)

8、 阅读代码

  1. Promise.resolve(1)
  2. .then(res => {
  3. console.log(res);
  4. return 2;
  5. })
  6. .catch(err => {
  7. return 3;
  8. })
  9. .then(res => {
  10. console.log(res);
  11. });

9、 阅读代码

  1. Promise.resolve().then(() => {
  2. return new Error('error!!!')
  3. }).then(res => {
  4. console.log("then: ", res)
  5. }).catch(err => {
  6. console.log("catch: ", err)
  7. })

10、 阅读代码

  1. const promise = Promise.resolve().then(() => {
  2. return promise;
  3. })
  4. promise.catch(console.err)

11、 阅读代码

  1. Promise.resolve(1)
  2. .then(2)
  3. .then(Promise.resolve(3))
  4. .then(console.log)

12、 阅读代码

  1. Promise.reject('err!!!')
  2. .then((res) => {
  3. console.log('success', res)
  4. }, (err) => {
  5. console.log('error', err)
  6. }).catch(err => {
  7. console.log('catch', err)
  8. })

13、 阅读代码

  1. Promise.resolve('1')
  2. .then(res => {
  3. console.log(res)
  4. })
  5. .finally(() => {
  6. console.log('finally')
  7. })
  8. Promise.resolve('2')
  9. .finally(() => {
  10. console.log('finally2')
  11. return '我是finally2返回的值'
  12. })
  13. .then(res => {
  14. console.log('finally2后面的then函数', res)
  15. }) 6

14、 阅读代码

  1. function runAsync (x) {
  2. const p = new Promise(r => setTimeout(() => r(x, console.log(x)), 1000))
  3. return p
  4. }
  5. Promise.all([runAsync(1), runAsync(2), runAsync(3)]).then(res => console.log(res))

15、 阅读代码

  1. function runAsync (x) {
  2. const p = new Promise(r => setTimeout(() => r(x, console.log(x)), 1000))
  3. return p
  4. }
  5. function runReject (x) {
  6. const p = new Promise((res, rej) => setTimeout(() => rej(`Error: ${x}`, console.log(x)), 1000 * x))
  7. return p
  8. }
  9. Promise.all([runAsync(1), runReject(4), runAsync(3), runReject(2)])
  10. .then(res => console.log(res))
  11. .catch(err => console.log(err))

16、 阅读代码

  1. function runAsync (x) {
  2. const p = new Promise(r => setTimeout(() => r(x, console.log(x)), 1000))
  3. return p
  4. }
  5. Promise.race([runAsync(1), runAsync(2), runAsync(3)])
  6. .then(res => console.log('result: ', res))
  7. .catch(err => console.log(err))

17、 阅读代码

  1. function runAsync(x) {
  2. const p = new Promise(r =>
  3. setTimeout(() => r(x, console.log(x)), 1000)
  4. );
  5. return p;
  6. }
  7. function runReject(x) {
  8. const p = new Promise((res, rej) =>
  9. setTimeout(() => rej(`Error: ${x}`, console.log(x)), 1000 * x)
  10. );
  11. return p;
  12. }
  13. Promise.race([runReject(0), runAsync(1), runAsync(2), runAsync(3)])
  14. .then(res => console.log("result: ", res))
  15. .catch(err => console.log(err));

18、 阅读代码

  1. async function async1() {
  2. console.log("async1 start");
  3. await async2();
  4. console.log("async1 end");
  5. }
  6. async function async2() {
  7. console.log("async2");
  8. }
  9. async1();
  10. console.log('start')

19、 阅读代码

  1. async function async1() {
  2. console.log("async1 start");
  3. await async2();
  4. console.log("async1 end");
  5. setTimeout(() => {
  6. console.log('timer1')
  7. }, 0)
  8. }
  9. async function async2() {
  10. setTimeout(() => {
  11. console.log('timer2')
  12. }, 0)
  13. console.log("async2");
  14. }
  15. async1();
  16. setTimeout(() => {
  17. console.log('timer3')
  18. }, 0)
  19. console.log("start")

20、 阅读代码

  1. async function async1 () {
  2. console.log('async1 start');
  3. await new Promise(resolve => {
  4. console.log('promise1')
  5. })
  6. console.log('async1 success');
  7. return 'async1 end'
  8. }
  9. console.log('srcipt start')
  10. async1().then(res => console.log(res))
  11. console.log('srcipt end')

21、 阅读代码

  1. async function async1 () {
  2. console.log('async1 start');
  3. await new Promise(resolve => {
  4. console.log('promise1')
  5. resolve('promise1 resolve')
  6. }).then(res => console.log(res))
  7. console.log('async1 success');
  8. return 'async1 end'
  9. }
  10. console.log('srcipt start')
  11. async1().then(res => console.log(res))
  12. console.log('srcipt end')

22、 阅读代码

  1. async function async1() {
  2. console.log("async1 start");
  3. await async2();
  4. console.log("async1 end");
  5. }
  6. async function async2() {
  7. console.log("async2");
  8. }
  9. console.log("script start");
  10. setTimeout(function() {
  11. console.log("setTimeout");
  12. }, 0);
  13. async1();
  14. new Promise(resolve => {
  15. console.log("promise1");
  16. resolve();
  17. }).then(function() {
  18. console.log("promise2");
  19. });
  20. console.log('script end')

23、 阅读代码

  1. async function async1 () {
  2. await async2();
  3. console.log('async1');
  4. return 'async1 success'
  5. }
  6. async function async2 () {
  7. return new Promise((resolve, reject) => {
  8. console.log('async2')
  9. reject('error')
  10. })
  11. }
  12. async1().then(res => console.log(res))

24、 阅读代码

  1. const first = () => (new Promise((resolve, reject) => {
  2. console.log(3);
  3. let p = new Promise((resolve, reject) => {
  4. console.log(7);
  5. setTimeout(() => {
  6. console.log(5);
  7. resolve(6);
  8. console.log(p)
  9. }, 0)
  10. resolve(1);
  11. });
  12. resolve(2);
  13. p.then((arg) => {
  14. console.log(arg);
  15. });
  16. }));
  17. first().then((arg) => {
  18. console.log(arg);
  19. });
  20. console.log(4);

25、 阅读代码

  1. const async1 = async () => {
  2. console.log('async1');
  3. setTimeout(() => {
  4. console.log('timer1')
  5. }, 2000)
  6. await new Promise(resolve => {
  7. console.log('promise1')
  8. })
  9. console.log('async1 end')
  10. return 'async1 success'
  11. }
  12. console.log('script start');
  13. async1().then(res => console.log(res));
  14. console.log('script end');
  15. Promise.resolve(1)
  16. .then(2)
  17. .then(Promise.resolve(3))
  18. .catch(4)
  19. .then(res => console.log(res))
  20. setTimeout(() => {
  21. console.log('timer2')
  22. }, 1000)

26、 阅读代码

  1. const p1 = new Promise((resolve) => {
  2. setTimeout(() => {
  3. resolve('resolve3');
  4. console.log('timer1')
  5. }, 0)
  6. resolve('resovle1');
  7. resolve('resolve2');
  8. }).then(res => {
  9. console.log(res) // resolve1
  10. setTimeout(() => {
  11. console.log(p1)
  12. }, 1000)
  13. }).finally(res => {
  14. console.log('finally', res)
  15. })

27、 阅读代码

  1. console.log('1');
  2. setTimeout(function() {
  3. console.log('2');
  4. process.nextTick(function() {
  5. console.log('3');
  6. })
  7. new Promise(function(resolve) {
  8. console.log('4');
  9. resolve();
  10. }).then(function() {
  11. console.log('5')
  12. })
  13. })
  14. process.nextTick(function() {
  15. console.log('6');
  16. })
  17. new Promise(function(resolve) {
  18. console.log('7');
  19. resolve();
  20. }).then(function() {
  21. console.log('8')
  22. })
  23. setTimeout(function() {
  24. console.log('9');
  25. process.nextTick(function() {
  26. console.log('10');
  27. })
  28. new Promise(function(resolve) {
  29. console.log('11');
  30. resolve();
  31. }).then(function() {
  32. console.log('12')
  33. })
  34. })

28、 阅读代码

  1. console.log(1)
  2. setTimeout(() => {
  3. console.log(2)
  4. })
  5. new Promise(resolve => {
  6. console.log(3)
  7. resolve(4)
  8. }).then(d => console.log(d))
  9. setTimeout(() => {
  10. console.log(5)
  11. new Promise(resolve => {
  12. resolve(6)
  13. }).then(d => console.log(d))
  14. })
  15. setTimeout(() => {
  16. console.log(7)
  17. })
  18. console.log(8)

29、 阅读代码

  1. console.log(1);
  2. setTimeout(() => {
  3. console.log(2);
  4. Promise.resolve().then(() => {
  5. console.log(3)
  6. });
  7. });
  8. new Promise((resolve, reject) => {
  9. console.log(4)
  10. resolve(5)
  11. }).then((data) => {
  12. console.log(data);
  13. })
  14. setTimeout(() => {
  15. console.log(6);
  16. })
  17. console.log(7);

30、 阅读代码

  1. Promise.resolve().then(() => {
  2. console.log('1');
  3. throw 'Error';
  4. }).then(() => {
  5. console.log('2');
  6. }).catch(() => {
  7. console.log('3');
  8. throw 'Error';
  9. }).then(() => {
  10. console.log('4');
  11. }).catch(() => {
  12. console.log('5');
  13. }).then(() => {
  14. console.log('6');
  15. });

31、 阅读代码

  1. setTimeout(function () {
  2. console.log(1);
  3. }, 100);
  4. new Promise(function (resolve) {
  5. console.log(2);
  6. resolve();
  7. console.log(3);
  8. }).then(function () {
  9. console.log(4);
  10. new Promise((resove, reject) => {
  11. console.log(5);
  12. setTimeout(() => {
  13. console.log(6);
  14. }, 10);
  15. })
  16. });
  17. console.log(7);
  18. console.log(8);