Skip to content

Instantly share code, notes, and snippets.

@mattiaslundberg
Created August 4, 2019 09:52
Show Gist options
  • Select an option

  • Save mattiaslundberg/b7d6b6b0933bb59bc4aa2b9f29da0ccc to your computer and use it in GitHub Desktop.

Select an option

Save mattiaslundberg/b7d6b6b0933bb59bc4aa2b9f29da0ccc to your computer and use it in GitHub Desktop.

Revisions

  1. mattiaslundberg created this gist Aug 4, 2019.
    20 changes: 20 additions & 0 deletions promises.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // Create new promise
    const p = new Promise((resolve, reject) => {
    reject(new Error('some error'));
    //resolve('Some data');
    });

    // Then/catch
    p.then(result => console.log(result)).catch(error => console.error(error));

    // Async
    const foo = async () => {
    try {
    const result = await p;
    console.log(result);
    } catch (error) {
    console.log('it crashed', error);
    }
    };

    foo();