Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Created May 3, 2016 04:06
Show Gist options
  • Select an option

  • Save slugbyte/ea0235e44f41b604cc6c003ef1d9c0d1 to your computer and use it in GitHub Desktop.

Select an option

Save slugbyte/ea0235e44f41b604cc6c003ef1d9c0d1 to your computer and use it in GitHub Desktop.

handling async data

index.js

'use strict';

const getData = require(__dirname + '/lib/async-get-data');


getData(function(err, data){
  if (err){
    console.error('grr someting borked');
    return;
  }
  console.log('getData() passed back: ', data);
});

lib/async-get-data.js

'use strict';

function fakeAsyncNodeCall(callback){
  process.nextTick(function(){
    const secretString = 'secret string';
    callback(null, secretString);
  });
}

module.exports = function(callback){
  fakeAsyncNodeCall(function(err, data){
    callback(err, data);
  });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment