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.
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With