示例

  1. Promise.resovle.then(() => {
  2. console.log(1)
  3. return Promise.resolve(100)
  4. }).then(res => {
  5. console.log(res)
  6. }).then(() => {
  7. console.log(200)
  8. }).then(() => {
  9. console.log(300)
  10. }).then(() => {
  11. console.log(400)
  12. }).then(() => {
  13. console.log(500)
  14. }).then(() => {
  15. console.log(600)
  16. }).then(() => {
  17. console.log(700)
  18. })
  19. Promise.resovle.then(() => {
  20. console.log(10)
  21. }).then(() => {
  22. console.log(20)
  23. }).then(() => {
  24. console.log(30)
  25. }).then(() => {
  26. console.log(40)
  27. }).then(() => {
  28. console.log(50)
  29. }).then(() => {
  30. console.log(60)
  31. }).then(() => {
  32. console.log(70)
  33. })
  34. //结果
  35. // 1 10 20 30 100 40 200 50 300 60 400 70 500 600 700

原理

  1. Promise.resovle.then(() => {
  2. console.log(1)
  3. })
  4. Promise.resolve().then(() => {
  5. //第一拍
  6. const p = Promise.resolve(100)
  7. Promise.resolve.then(() => {
  8. //第二拍
  9. p.then(res => console.log(res))
  10. .then(res => console.log(200))
  11. .then(res => console.log(300))
  12. .then(res => console.log(400))
  13. .then(res => console.log(500))
  14. .then(res => console.log(600))
  15. .then(res => console.log(700))
  16. })
  17. })
  18. Promise.resovle.then(() => {
  19. console.log(10)
  20. }).then(() => {
  21. console.log(20)
  22. }).then(() => {
  23. console.log(30)
  24. }).then(() => {
  25. console.log(40)
  26. }).then(() => {
  27. console.log(50)
  28. }).then(() => {
  29. console.log(60)
  30. }).then(() => {
  31. console.log(70)
  32. })
  33. //结果
  34. // 1 10 20 30 100 40 200 50 300 60 400 70 500 600 700