rss
0

JavaScript ES6: Generators + Promises

  • Sumo

es6-promiseEverything you wanted to know about JavaScript ES6 new features is pretty much covered in this article ES6 in Depth.

My mostly beloved staff that I already use heavily on my NodeJS productions.
Also I found a good usage of generators+promises, I always wanted to have simple solution for if-else switching in long Promise chaining.
Now we can do it via generator+promise combination with Yield keyword. The long story with detailed explanation is here: ES6 Generators and asynchronous javascript

 

 

 

// function shorthand
items.map(item => {
  console.log(item)
})

// generators + promises
getAndSaveCrawlItemSocialScore (clink, type, date) {
  var self = this
  return Promise.coroutine(function * () {
    let link = yield models.links.getLinkById(clink.lid)

    if (!link) {
      yield models.crawlCron.remove(clink._id)
      console.log('No link for CID: ' + clink._id)
      yield Promise.resolve(true)
    }else {
      let score = yield self.grabLinkScore(link)
      yield self.saveLinkScore(link._id, score)
      yield models.links.markAsParsed(link)
      yield self.CrawlCron.markAsParsed(clink)
      yield Promise.resolve()
    }
  })()
}

About the Author

Edgar Marukyan is an expert of website development and performance optimization, who maintains and consults clients with high load traffic websites.. If you need an assistance, feel free to drop me a line.         Website - Twitter - Facebook

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.