Skip to content

Instantly share code, notes, and snippets.

@numtel
Created April 26, 2015 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numtel/c8f9b883b4bbbfab6abd to your computer and use it in GitHub Desktop.
Save numtel/c8f9b883b4bbbfab6abd to your computer and use it in GitHub Desktop.
Javascript Async style memory usage

Async/await style (i.e. how most of this package's code is written)

module.exports = async function() {
  await delay(1)
}

function delay(duration) {
  return new Promise(resolve => setTimeout(resolve, duration))
}

async-await

Promise without async/await

module.exports = function() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1)
  })
}

promise

Simple callback

exports.runner = function(cb) {
  setTimeout(cb, 1)
}

callback

Fibers/futures

var Future = require('fibers/future')

exports.fiberRunner = function() {
  var fut = new Future

  setTimeout(function() {
    fut['return']()
  }, 1)

  return fut.wait()
}

fibers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment