-2

I've got following code:

function generateId() {
        var id = Math.floor(Math.random()*1000);
        var lastId = this.getLastAdvertId();
        var self = this;

        var promise = when.promise(function(resolve, reject) {

            if(id === lastId) self.generateAdvertId();

            if(lastId !== null){
                self.compareImages(id, lastId).then(function(result){
                    if(result) self.generateAdvertId();
                    else{
                        self.setLastAdvertId(id);
                        resolve(id);
                    }
                });
            }

            self.setLastAdvertId(id);
            resolve(id);

        });

        return promise;
}

This code 'works' the issue I've got is in that part:

if(lastId !== null){
    self.compareImages(id, lastId).then(function(result){
        if(result) self.generateAdvertId();
        else{
            self.setLastAdvertId(id);
            resolve(id);
        }
    });
}

when both images are the same result is true, but there isn't generated new Id but returned one that was generated. What may i doing wrong there?

Abdizriel
  • 345
  • 1
  • 4
  • 20
  • 1
    The `id` variable that is assigned in the second line of your first code sample is not modified anywhere else in the code. Is that what you were expecting. What does `self.generateAdvertId` do? – Jack A. Sep 20 '15 at 17:35
  • 1
    Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Sep 21 '15 at 10:49
  • The problem is the final `resolve(id)` is always being executed, so the `resolve(id)` in the `self.compareImages ...then` wont change the value of the fullfilled Promise, as a Promise can only be resolved or rejected once - it also looks like you should be doing something like `id = self.generateAdvertId();` instead of just `self.generateAdvertId();` - but it's hard to say – Jaromanda X Sep 26 '15 at 08:15

1 Answers1

0

Without knowing the specifics of self.generateAdvertId and self.compareImages functions, I can only guess this is what you need to do

I'm assuming self.generateAdvertId will return an id - otherwise your code doesn't make that much sense - again, only seeing a snippet may be the reason for that

function generateId() {
    var id = Math.floor(Math.random()*1000);
    var lastId = this.getLastAdvertId();
    var self = this;

    if (id === lastId) {
        id = self.generateAdvertId();
    }

    if (lastId !== null) {
        return self.compareImages(id, lastId).then(function(result){
            if (result) {
                id = self.generateAdvertId(); // your original code looked like you (incorrectly) expected it to fall through to the setLastAdvertId/resolve at the bottom of the function,
            }
            self.setLastAdvertId(id);
            resolve(id);
        }); // this returns a promise
    }

    self.setLastAdvertId(id);
    return when.resolve(id); // return a promise with the resolved value = id
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87