Reputation: 375
I have some issues with promise usage (coming from callback horror)
I created a Fiddle but its not working... this is how I understood that it worked
1) create for each async promise 2) use then after each of the async
var a = new Tester("male");
var test = a.getFirst("test1").then("test2").then("test3");
console.log(test);
it does nothing...
https://jsfiddle.net/awzh91yv/
Upvotes: 0
Views: 49
Reputation: 10458
var a = new Tester("male");
var test = a.getFirst(function(){
return "test1";
})
.then(function(){
return "test2";
})
.then(function(){
return "test3";
})
.then(function(test){
console.log(test);
});
This would give the desired result since it waits for the promise to finish
Upvotes: 0
Reputation: 73211
That's not how you chain a promise. You'd call a function in the then
, it doesn't magically call your class' methods in order you created them. Try
a.getFirst("foo")
.then(() => a.getSecond("bar"))
.then(() => a.getLast("baz"))
.then(console.log);
Above being said, that's not something you'd use a promise for.
Upvotes: 1
Reputation: 2090
You're not passing your promise values along the chain. .then()
needs to be passed the value - so it'd look something like .then(val => a.getSecond("Jim")) ...
etc
I've updated your fiddle to work somewhat.
https://jsfiddle.net/awzh91yv/1/
Upvotes: 1