Promise.prototype.then/catch

  1. Promise.prototype.then / Promise.prototype.catch
  2. function getIp() {
  3. var promise = new Promise(function(resolve, reject){
  4. var xhr = new XMLHttpRequest()
  5. xhr.open('GET', 'https://easy-mock.com/mock/5ac2f80c3d211137b3f2843a/promise/getIp', true)
  6. xhr.onload = function(){
  7. var retJson = JSON.parse(xhr.responseText) // {"ip":"58.100.211.137"}
  8. resolve(retJson.ip)
  9. }
  10. xhr.onerror = function(){
  11. reject('获取IP失败')
  12. }
  13. xhr.send()
  14. })
  15. return promise
  16. }
  17. function getCityFromIp(ip) {
  18. var promise = new Promise(function(resolve, reject){
  19. var xhr = new XMLHttpRequest()
  20. xhr.open('GET', 'https://easy-mock.com/mock/5ac2f80c3d211137b3f2843a/promise/getCityFromIp?ip='+ip, true)
  21. xhr.onload = function(){
  22. var retJson = JSON.parse(xhr.responseText) // {"city": "hangzhou","ip": "23.45.12.34"}
  23. resolve(retJson.city)
  24. }
  25. xhr.onerror = function(){
  26. reject('获取city失败')
  27. }
  28. xhr.send()
  29. })
  30. return promise
  31. }
  32. function getWeatherFromCity(city) {
  33. var promise = new Promise(function(resolve, reject){
  34. var xhr = new XMLHttpRequest()
  35. xhr.open('GET', 'https://easy-mock.com/mock/5ac2f80c3d211137b3f2843a/promise/getWeatherFromCity?city='+city, true)
  36. xhr.onload = function(){
  37. var retJson = JSON.parse(xhr.responseText) //{"weather": "晴天","city": "beijing"}
  38. resolve(retJson)
  39. }
  40. xhr.onerror = function(){
  41. reject('获取天气失败')
  42. }
  43. xhr.send()
  44. })
  45. return promise
  46. }
  47. getIp().then(function(ip){
  48. return getCityFromIp(ip)
  49. }).then(function(city){
  50. return getWeatherFromCity(city)
  51. }).then(function(data){
  52. console.log(data)
  53. }).catch(function(e){
  54. console.log('出现了错误', e)
  55. })

Promise一个构造函数,.then和.catch方法都在prototype上

Promise.all

  1. function getCityFromIp(ip) {
  2. var promise = new Promise(function(resolve, reject){
  3. var xhr = new XMLHttpRequest()
  4. xhr.open('GET', 'https://easy-mock.com/mock/5ac2f80c3d211137b3f2843a/promise/getCityFromIp?ip='+ip, true)
  5. xhr.onload = function(){
  6. var retJson = JSON.parse(xhr.responseText) // {"city": "hangzhou","ip": "23.45.12.34"}
  7. resolve(retJson)
  8. }
  9. xhr.onerror = function(){
  10. reject('获取city失败')
  11. }
  12. xhr.send()
  13. })
  14. return promise
  15. }
  16. var p1 = getCityFromIp('10.10.10.1')
  17. var p2 = getCityFromIp('10.10.10.2')
  18. var p3 = getCityFromIp('10.10.10.3')
  19. //Promise.all, 当所有的 Promise 对象都完成后再执行
  20. Promise.all([p1, p2, p3]).then(data=>{
  21. console.log(data)
  22. })

当所有promise都settle, 全都resolved则执行.then第一个回调函数,返回所有结果组成的数组,只要有一个rejected, 则执行第二个回调函数

Promise.race

  1. function getCityFromIp(ip) {
  2. var promise = new Promise(function(resolve, reject){
  3. var xhr = new XMLHttpRequest()
  4. xhr.open('GET', 'https://easy-mock.com/mock/5ac2f80c3d211137b3f2843a/promise/getCityFromIp?ip='+ip, true)
  5. xhr.onload = function(){
  6. var retJson = JSON.parse(xhr.responseText) // {"city": "hangzhou","ip": "23.45.12.34"}
  7. resolve(retJson)
  8. }
  9. xhr.onerror = function(){
  10. reject('获取city失败')
  11. }
  12. setTimeout(()=>{
  13. xhr.send()
  14. }, Math.random()*1000)
  15. })
  16. return promise
  17. }
  18. var p1 = getCityFromIp('10.10.10.1')
  19. var p2 = getCityFromIp('10.10.10.2')
  20. var p3 = getCityFromIp('10.10.10.3')
  21. //Promise.all, 当所有的 Promise 对象都完成后再执行
  22. Promise.race([p1, p2, p3]).then(data=>{
  23. console.log(data)
  24. })

只要有一个settle, resolved则执行.then第一个回调函数,rejected则执行第二个回调函数

  1. function fn1() {
  2. return new Promise((resolve, reject)=>{
  3. setTimeout(()=>{
  4. console.log('fn1...')
  5. resolve()
  6. }, 1000)
  7. })
  8. }
  9. function fn2() {
  10. return new Promise((resolve, reject)=>{
  11. setTimeout(()=>{
  12. console.log('fn2...')
  13. resolve()
  14. }, 1000)
  15. })
  16. }
  17. function fn3() {
  18. return new Promise((resolve, reject)=>{
  19. setTimeout(()=>{
  20. console.log('fn3...')
  21. resolve()
  22. }, 1000)
  23. })
  24. }
  25. function onerror() {
  26. console.log('error')
  27. }
  28. fn1().then(fn2).then(fn3).catch(onerror)