Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // some basic promises
- function firstMethod (initialStuff) {
- let thisPromise = new Promise((resolve, reject) => {
- let failflag = false
- if( failflag ) {
- setTimeout(() => reject('Failure from first promisingOperation!'), 500)
- } else {
- setTimeout(() => {
- resolve(initialStuff)
- }, 200)
- }
- })
- return thisPromise
- }
- function secondMethod(someStuff) {
- let thisPromise = new Promise((resolve, reject) => {
- let failflag = false
- if( failflag ) {
- setTimeout(() => reject('Failure from second promisingOperation!'), 500)
- } else {
- setTimeout(() => {
- resolve({data: { firstData: someStuff.data, secondData: '456'}})
- }, 200)
- }
- })
- return thisPromise
- }
- function thirdMethod(someStuff) {
- let thisPromise = new Promise((resolve, reject) => {
- let failflag = false
- if( failflag ) {
- setTimeout(() => reject('Failure from third promisingOperation!'), 500)
- } else {
- setTimeout(() => {
- resolve({data: { firstData: someStuff.data.firstData, secondData: someStuff.data.secondData, thirdData: '789'}})
- }, 200)
- }
- })
- return thisPromise
- }
- // invocation
- let chainStatus = 2
- firstMethod({data: '123'})
- .then(secondMethod)
- .then(thirdMethod)
- .then((rets) => {
- chainStatus = 0
- console.log(`Chainer succeeded! :`,rets)
- })
- .catch((error) => {
- chainStatus = 1
- console.log(`Chainer failed! :`,error)
- })
- .then(() => {
- if (chainStatus === 0) {
- console.log('all done deal with success')
- } else if (chainStatus === 1) {
- console.log('all done deal with fail')
- } else {
- console.log('all done something unexpected occured, deal with that')
- }
- })
Add Comment
Please, Sign In to add comment