MichaelAngelo
MichaelAngelo

Reputation: 375

Promise confusion

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

Answers (3)

marvel308
marvel308

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

JSFIDDLE

Upvotes: 0

baao
baao

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);

Updated fiddle

Above being said, that's not something you'd use a promise for.

Upvotes: 1

Christopher Messer
Christopher Messer

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

Related Questions