Skip to content

Instantly share code, notes, and snippets.

@andrewlistopadov
Last active November 16, 2018 09:07
Show Gist options
  • Select an option

  • Save andrewlistopadov/53b0ec622be7848466084b5fcd90402d to your computer and use it in GitHub Desktop.

Select an option

Save andrewlistopadov/53b0ec622be7848466084b5fcd90402d to your computer and use it in GitHub Desktop.

Revisions

  1. andrewlistopadov revised this gist Nov 16, 2018. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions unhandledPromiseRejection.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function func() {
    function getRejectedPromise() {
    const promise = new Promise((resolve, reject) => {
    reject('some error');
    });
    @@ -8,10 +8,14 @@ function func() {
    let counter = 0;
    const interval = setInterval(() => {
    if (++counter > 10) clearInterval(interval);
    else console.log(`count: ${counter}`);
    else console.log(`async counter: ${counter}`);
    }, 500);

    func()
    .then(() => console.log('never be called'));
    getRejectedPromise()
    .then(() => console.log('Never be called. But rejection will not be visible in the console and main thread will be alive.'));

    console.log('will be reached');
    console.log('will be reached');

    setTimeout(() => console.log('2nd async console output'), 3000);
    setTimeout(() => console.log('1 async console output'), 0);
    setTimeout(() => console.log('last async console output'), 6000);
  2. andrewlistopadov renamed this gist Nov 16, 2018. 1 changed file with 0 additions and 0 deletions.
  3. andrewlistopadov created this gist Nov 16, 2018.
    17 changes: 17 additions & 0 deletions unhandled promise rejection
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    function func() {
    const promise = new Promise((resolve, reject) => {
    reject('some error');
    });
    return promise;
    }

    let counter = 0;
    const interval = setInterval(() => {
    if (++counter > 10) clearInterval(interval);
    else console.log(`count: ${counter}`);
    }, 500);

    func()
    .then(() => console.log('never be called'));

    console.log('will be reached');