0

I'm trying to resolve a Promise in a outer function that is called as an event listener. The problem is, the Promise does not wait the resolve callback at all.

Also, please keep in mind that I need a solution using plain ES5.

Calling:

render(map)
.then(function(road){ return paint(road, map, imgs) })
.then(function(){ asyncListen(keyhandler, ['numeric']) }) //should waiy by user input...
.then(function(r){ console.log('resolved: ' + r) }); //... but this logs undefined before it

asyncListener:

function asyncListen(handler, keys){
    return new Promise(function(resolve, reject){
        var keyhandler = handler.call(this, resolve);
        document.addEventListener("keyup", keyhandler);
        keys.forEach(function(key){ 
            var upperKey = key.toUpperCase();
            var objKey = iAppLib.keys[upperKey];        
            upperKey === 'NUMERIC' ? iAppLib.registerRcuEvent(objKey, keyhandler) : iAppLib.registerRcuEvent(objKey);
        });

        iAppLib.registerAllRcuEvents(function() { return false; });
    });
}

keyhandler:

function keyhandler(resolve){
    return function(num){
        if (num >= 1 && num <= 4){
            resolve(num);
        }   
    }
}

I've already tried some workarounds, like wrapping both de event and the keyhandler in Promises, but it didn't work out. Same for the solutions here and here.

Henrique Guerra
  • 233
  • 1
  • 13

1 Answers1

-1

Return asyncListen in:

render(map)
.then(function(road){ return paint(road, map, imgs) })
.then(function(){ return asyncListen(keyhandler, ['numeric']) }) //should waiy by user input...
.then(function(r){ console.log('resolved: ' + r) }); 
rafaelgomesxyz
  • 1,405
  • 8
  • 14