Jamie Ide
Jamie Ide

Reputation: 49261

Why does Q's thenResolve method return undefined?

Why isn't thenResolve working as I expect?

I have a method in a javascript module:

function addVisit(companyId) {
    var  newVisit;

    return getInventoryItems()
        .then(function(data) {
            newVisit = createVisit(companyId, data);
        })
        .then(function() {
            breezeVisitsManager.saveChanges();
        })
        .thenResolve(newVisit);
}

That is called by another module:

visitRepository.addVisit(self.companyId)
    .then(function(newVisit) {
        var route = self.visitRoute(newVisit.VisitId());
        router.navigate(route);
     }

newVisit exists at the time thenResolve is called, but it's undefined when the calling code receives it. I've played around with the sample JSFiddle and I don't understand why my code isn't working.

Upvotes: 0

Views: 1185

Answers (1)

Esailija
Esailija

Reputation: 140228

newVisit is undefined at the time you pass it to .thenResolve (immediately when addVisit is called).

function addVisit(companyId) {
    var  newVisit;

    return getInventoryItems()
        .then(function(data) {
            newVisit = createVisit(companyId, data);
        })
        .then(function() {
            breezeVisitsManager.saveChanges();
        })
        .then(function(){
            return newVisit;
        });
}

Promises don't change the language, a.b.c() will still call c() immediately no matter what.

Upvotes: 4

Related Questions