Created
August 29, 2020 11:45
-
-
Save ashishtiwari1993/4f60511db7ead6c768e47b199a0a854d to your computer and use it in GitHub Desktop.
Javascript promises short example
This file contains 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
function one(){ | |
var promise = new Promise(function(resolve, reject){ | |
setTimeout(function(){ | |
console.log(1) | |
resolve() | |
}, 998) | |
}) | |
return promise | |
} | |
function two(){ | |
var promise = new Promise(function(resolve, reject){ | |
setTimeout(function(){ | |
console.log(2) | |
resolve("two-param") | |
}, 999) | |
}) | |
return promise | |
} | |
function three(param){ | |
var promise = new Promise(function(resolve, reject){ | |
setTimeout(function(){ | |
console.log(3) | |
console.log("Receving param from function two() ", param); | |
resolve() | |
}, 1000) | |
}) | |
return promise | |
} | |
function four(){ | |
var promise = new Promise(function(resolve, reject){ | |
setTimeout(function(){ | |
console.log(4) | |
resolve() | |
}, 900) | |
}) | |
return promise | |
} | |
function five(){ | |
var promise = new Promise(function(resolve, reject){ | |
setTimeout(function(){ | |
console.log(5) | |
reject("Something wrong...") | |
}, 995) | |
}) | |
return promise | |
} | |
function six(){ | |
var promise = new Promise(function(resolve, reject){ | |
setTimeout(function(){ | |
console.log(6) | |
resolve() | |
}, 800) | |
}) | |
return promise | |
} | |
function errorHandle(err){ | |
console.log(err) | |
} | |
one() | |
.then(two) | |
.then(three) | |
.then(four) | |
.then(five) | |
.then(six) | |
.catch(errorHandle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment