Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/await #14

Open
mhulse opened this issue Feb 9, 2019 · 10 comments
Open

async/await #14

mhulse opened this issue Feb 9, 2019 · 10 comments
Labels

Comments

@mhulse
Copy link
Owner

mhulse commented Feb 9, 2019

https://davidwalsh.name/async-await

Parse out the goods from there.

@mhulse
Copy link
Owner Author

mhulse commented Feb 22, 2019

Good general rule of thumb:

Always use try..catch for await blocks, if you don't want to rethrow exception upper.

Example:

async function listDir() {
  try {
    return await fsPromises.readdir('path/to/dir');
  } catch (err) {
    console.error('Error occured while reading directory!', err);
  }
}

Or:

// Async/Await:
async function copyFiles () {
  try {
    await fs.copy('/tmp/myfile', '/tmp/mynewfile')
    console.log('success!')
  } catch (err) {
    console.error(err)
  }
}

@mhulse mhulse added the WIKI label Feb 22, 2019
@mhulse
Copy link
Owner Author

mhulse commented Feb 25, 2019

@mhulse
Copy link
Owner Author

mhulse commented Feb 26, 2019

@mhulse
Copy link
Owner Author

mhulse commented Feb 27, 2019

let isOurPromiseFinished = false;
const myAsyncAwaitBlock = async (str) => {
  try {
    // If the promise resolves, we enter this code block
    const myPromise = await returnsAPromise(str);
    console.log(`using async/await, ${res}`);
  } catch(err) {
    // If the promise rejects, we enter this code block
    console.log(err);
  } finally {
    /* This is for code that doesn't rely on the outcome of the    
    promise but still needs to run once it's handled */
    isOurPromiseFinished = true;
  }
}
myAsyncAwaitBlock(myFirstString);

https://levelup.gitconnected.com/async-await-vs-promises-4fe98d11038f

@mhulse
Copy link
Owner Author

mhulse commented Feb 27, 2019

@mhulse
Copy link
Owner Author

mhulse commented Feb 27, 2019

@mhulse
Copy link
Owner Author

mhulse commented Mar 3, 2019

@mhulse
Copy link
Owner Author

mhulse commented Mar 3, 2019

Error handling tip:

https://www.bennadel.com/blog/2831-rethrowing-errors-in-javascript-and-node-js.htm

Re-throwing helps for debug.

@mhulse
Copy link
Owner Author

mhulse commented Mar 4, 2019

Re-throwing errors may not be needed:

https://stackoverflow.com/a/44473570/922323

@mhulse
Copy link
Owner Author

mhulse commented Mar 4, 2019

TCF return value:

https://stackoverflow.com/a/3838054/922323

Good to see/read comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant