Guest User

Untitled

a guest
Mar 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. // some basic promises
  2. function firstMethod (initialStuff) {
  3. let thisPromise = new Promise((resolve, reject) => {
  4. let failflag = false
  5. if( failflag ) {
  6. setTimeout(() => reject('Failure from first promisingOperation!'), 500)
  7. } else {
  8. setTimeout(() => {
  9. resolve(initialStuff)
  10. }, 200)
  11. }
  12. })
  13. return thisPromise
  14. }
  15.  
  16. function secondMethod(someStuff) {
  17. let thisPromise = new Promise((resolve, reject) => {
  18. let failflag = false
  19. if( failflag ) {
  20. setTimeout(() => reject('Failure from second promisingOperation!'), 500)
  21. } else {
  22. setTimeout(() => {
  23. resolve({data: { firstData: someStuff.data, secondData: '456'}})
  24. }, 200)
  25. }
  26. })
  27. return thisPromise
  28. }
  29. function thirdMethod(someStuff) {
  30. let thisPromise = new Promise((resolve, reject) => {
  31. let failflag = false
  32. if( failflag ) {
  33. setTimeout(() => reject('Failure from third promisingOperation!'), 500)
  34. } else {
  35. setTimeout(() => {
  36. resolve({data: { firstData: someStuff.data.firstData, secondData: someStuff.data.secondData, thirdData: '789'}})
  37. }, 200)
  38. }
  39. })
  40. return thisPromise
  41. }
  42.  
  43. // invocation
  44. let chainStatus = 2
  45.  
  46. firstMethod({data: '123'})
  47. .then(secondMethod)
  48. .then(thirdMethod)
  49. .then((rets) => {
  50. chainStatus = 0
  51. console.log(`Chainer succeeded! :`,rets)
  52. })
  53. .catch((error) => {
  54. chainStatus = 1
  55. console.log(`Chainer failed! :`,error)
  56. })
  57. .then(() => {
  58. if (chainStatus === 0) {
  59. console.log('all done deal with success')
  60. } else if (chainStatus === 1) {
  61. console.log('all done deal with fail')
  62. } else {
  63. console.log('all done something unexpected occured, deal with that')
  64. }
  65. })
Add Comment
Please, Sign In to add comment