Created
April 24, 2016 15:09
-
-
Save vajahath/389a70d33ff5f9873eff7b87fc89b62e to your computer and use it in GitHub Desktop.
Node promise example with then() using npm q module
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
// require q lib | |
var q = require('q'); | |
// function deininitions | |
function sayone(){ | |
var d = q.defer(); | |
// function that takes time to finish | |
setTimeout(function(){ | |
console.log("sayone is called"); | |
// this is where you want to call the next function | |
d.resolve(); | |
}, 5000); | |
// return the promis | |
return d.promise; | |
}; | |
function saytwo(){ | |
var d = q.defer(); | |
setTimeout(function(){ | |
console.log("saytwo is called val = "); | |
d.resolve(); | |
}, 1000); | |
return d.promise; | |
}; | |
function saythree(){ | |
var d = q.defer(); | |
setTimeout(function(){ | |
console.log("saythree is called"); | |
d.resolve(); | |
}, 3000); | |
return d.promise; | |
}; | |
function sayfour(){ | |
var d = q.defer(); | |
setTimeout(function(){ | |
console.log("sayfour is called"); | |
d.resolve(); | |
}, 2000); | |
return d.promise; | |
}; | |
// var d = q.defer(); | |
sayone() | |
.then(saytwo) | |
.then(saythree) | |
.then(sayfour); | |
// | |
// sayone(); | |
// saytwo(); | |
// saythree(); | |
// sayfour(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment