.then/catch/finally .
.then/catch/finally .
:
let promise = Promise.resolve();
promise.then(() => alert(" !"));
alert(" "); // .
' , ' !' .
. .
.then ? .
. ECMA PromiseJobs (internal queue) . V8 ' (microtask queue)' .
- (FIFO, first-in-first-out).
- .
, .then/catch/finally . . .
' .
.
.then/catch/finally , . , .
' !' , ' ? .
. .then .
Promise.resolve()
.then(() => alert(" !"))
.then(() => alert(" "));
.
(unhandled rejection) .
' .
.catch .
let promise = Promise.reject(new Error(" !"));
promise.catch(err => alert('!'));
// .
window.addEventListener('unhandledrejection', event => alert(event.reason));
.catch , unhandledrejection .
let promise = Promise.reject(new Error(" !"));
// !
window.addEventListener('unhandledrejection', event => alert(event.reason));
setTimeout ?
let promise = Promise.reject(new Error(" !"));
setTimeout(() => promise.catch(err => alert('!')), 1000);
// Error: !
window.addEventListener('unhandledrejection', event => alert(event.reason));
! , ! .
" unhandledrejection ?" .
unhandledrejection . (rejected) unhandledrejection . .
setTimeout .catch . .catch unhandledrejection ! .
(ES8 ) (promise job) .
.then/catch/finally .
.then/catch/finally .then .
Node.js ' (event loop)' '(macrotask)' . , , .
<code>,<pre>. 10 plnkr, JSBin, codepen .