title | permalink | excerpt | last_modified_at |
---|---|---|---|
Promise is Asynchronous |
/the-modern-javascript-tutorial/promise-is-asynchronous |
Promise is asynchronous... |
2018-07-02 15:58:49 -0400 |
Promise is asynchronous.
Let's log a message before and after we call the promise as below:
// call our promise
var askMom = function () {
console.log('before asking Mom'); // Log before
willIGetNewPhone
.then(showOff)
.then(function (result) {
console.log(result);
})
.catch(function (error) {
console.log(error.message);
});
console.log('after asking mom'); // Log after
}
What is the sequence of expected output? You may expect:
- before asking Mom
- Hey friend, I have a new black Samsung phone.
- after asking mom
However, the actual output sequence is:
- before asking Mom
- after asking mom
- Hey friend, I have a new black Samsung phone.
Why? Because life (or JS) waits for no man. You, the kid, wouldn't stop playing while waiting for your mom promise (the new phone). Don't you?
That's something we call asynchronous, the code will run without blocking or waiting for the result. Anything that need to wait for promise to proceed, you put that in .then
.