[Vuejs]-JS Promise in vue shows as undefined

0👍

Your completed function is not returning a promise (it is returning undefined as you noticed).
You are returning the promise for the event emitter when the queue-action is called. You are defining a new function here: e => { and that function that is returning a promise is passed to the EventBus event emitter

0👍

You want to wrap the whole EventBus.$on() in your promise, like this:

export function completed()  {
    return new Promise((resolve) => {
        EventBus.$on('queue-action', e => {
            if(e.action == 'completed'){
                let item = e.queueItem
                resolve(item);
            }
        });
    });
}

As a rule of thumb, unless you have a very specific reason to do something else, a function returning a promise should have all it’s body wrapped in return new Promise(...);. It is also normal and ok to have a lot of code wrapped inside a promise.

Note to the code: I removed reject part both for brevity and because I’m not sure that is what you want to do. Unless it is an error if some action happens before 'completed', you should just ignore such an event.

Leave a comment