Promises are an abstraction for asynchronous programming. If you don’t know anything about them, I recommend that you first read asynchronous programming in JavaScript before you continue.
A promise is an object. It is not a function and it is not the value returned from the async operation. To get to the value, you need to call the
then
method on the promise object. You pass a callback function as an argument to then
. The promise invokes the callback and passes the value you’re interested in into the callback. Clear as mud, right?
Here’s a fictitious example that pretends like calculating a random number requires an async operation:
getRandomNumberAsync().then(function(someNumber) {
// do stuff with `someNumber`
});
Read full article here
No comments:
Post a Comment