Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Promises gaaaaanz einfach...
var Q = require('q');
var myPromDemo = {}
myPromDemo.func1 = function() {
//Deferred holen
var deferred = Q.defer();
console.log('Warte 10s');
setTimeout(function(){
console.log('Func1 ist fertig...');
deferred.resolve(true);
},10000);
return deferred.promise;
}
myPromDemo.func2 = function() {
//Deferred holen
var deferred = Q.defer();
console.log('Warte 5s');
setTimeout(function(){
console.log('Func2 ist fertig...');
deferred.resolve(true);
},5000);
return deferred.promise;
}
myPromDemo.func3 = function() {
//Deferred holen
var deferred = Q.defer();
console.log('Warte 30s');
setTimeout(function(){
console.log('Func3 ist fertig...');
deferred.resolve(true);
},30000);
return deferred.promise;
}
myPromDemo.demo = function() {
//Orchestration von Promises
myPromDemo.func1()
.then(function(){
console.log('Func1 sollte vorbei sein...');
return myPromDemo.func2();
})
.then(function(){
console.log('Func2 sollte vorbei sein...');
return myPromDemo.func3();
})
.then(function(){
console.log('Func3 sollte vorbei sein...');
})
}
module.exports = myPromDemo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment