Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create a promise for the end of playing sound?

Tags:

People also ask

What are the promises and how do they work?

A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it's not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending.

How do I return a promise from a function?

Promise resolve() method: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

What is resolve in promise?

resolve() The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.

What does a promise return?

Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.


I have a simple JavaScript that loads sounds:

  prefix = 'modules/sounds/';
 _sounds = ['nameOfSound','nameOfSound','nameOfSound'];


 for (var sound in _sounds) {
                if (_sounds.hasOwnProperty(sound)) {
                    var cached = _sounds[sound];
                    cached.audio = new Audio(prefix + cached.src);
                    }
                }
            }

All is being cached here, and then in angular I just have a service that does the following function:

 this.play = function(trackName) {
        _sounds[trackName].audio.play();
    };

What I want is a simple promise for the sound, so when I call it, I will have something like this:

soundService.play('boom').then(function(){
    do something here
});

Is that even possible?