title: Node + Sequelize: How to check if item exists before adding? (async confusion)
body: <p>I am unfortunately new to node and running into some confusion regarding the asynchronous/synchronous execution of node.</p>
  
  <p>I am using node, sequelize with sqlite and async.js.</p>
  
  <p>I have a series of <code>Articles</code>, each of which has a number of <code>Authors</code>.</p>
  
  <p>For each <code>Authors</code> in each <code>Article</code>, I'd like to check if the <code>Author</code> exists. If not, create it.</p>
  
  <p>The problem is, on the initial run, duplicate authors are being created, I assume due to asynchronous functionality causing an issue with checking for existence.</p>
  
  <p>For example, with the array: <code>authors = ['A. Test', 'B. Test', 'C. Test', 'A. Test']</code></p>
  
  <p>and the code:</p>
  
  <pre><code>async.each(authors, function(item, callback){
      Author.sync().then(function(){
        Author.count({ where: {name: item.trim()} }).then(function(count){
          if (count != 0) {
            console.log('Author already exists')
          } else {
            console.log('Creating author...')
            Author.create({
              name: item.trim()
            })
          }
        })
      })
    })
  </code></pre>
  
  <p>On the first run, will create a table:</p>
  
  <pre><code>ID | name
  ------------
  0  | A. Test
  1  | B. Test
  2  | C. Test
  3  | A. Test
  </code></pre>
  
  <p>What am I doing wrong? I seem to be missing a fundamental concept of asynchronous vs synchronous execution in Node.</p>
  
  <p>(I've also tried async.eachSeries which is supposed to execute in series rather than in parallel?)</p>
  
  <p><strong>Edit: Slightly refactored, but still creating duplicates</strong></p>
  
  <pre><code>async.eachSeries(authors, function(authorName, callback){
      Author.findOne({ where: {name: authorName.trim()} }).
      then(function(author){
        if (author) {
          // Author exists...
          callback()
        } else {
          // Author does not exist...
          Author.create({
            name: authorName.trim()
          }).then(function(author){
            callback()
          })
        }
      })
    })
  </code></pre>