Created
April 9, 2018 14:56
-
-
Save EricLondon/52f635a4e0e736a67735b018eb89b559 to your computer and use it in GitHub Desktop.
javascript chainable promise example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let method1 = function() { | |
console.log("method1") | |
return true | |
} | |
let method2 = function() { | |
console.log("method2") | |
return true | |
} | |
let method3 = function() { | |
console.log("method3") | |
return true | |
} | |
let chainableMethod1 = function() { | |
return new Promise(function(resolve, reject) { | |
let result = method1() | |
if (result) { | |
return resolve("method1") | |
} | |
else { | |
return reject("method1") | |
} | |
}); | |
} | |
let chainableMethod2 = function() { | |
return new Promise(function(resolve, reject) { | |
let result = method2() | |
if (result) { | |
return resolve("method2") | |
} | |
else { | |
return reject("method2") | |
} | |
}); | |
} | |
let chainableMethod3 = function() { | |
return new Promise(function(resolve, reject) { | |
let result = method3() | |
if (result) { | |
return resolve("method3") | |
} | |
else { | |
return reject("method3") | |
} | |
}); | |
} | |
chainableMethod1() | |
.then(chainableMethod2) | |
.then(chainableMethod3) | |
.then(function(res){ | |
console.log("done") | |
}) | |
.catch(function(err) { | |
console.log('error:', err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment