Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 974 Bytes

File metadata and controls

33 lines (24 loc) · 974 Bytes

Refactor the code

// ==========================
//   Inside fetch.js module
// ==========================

function asyncController(asyncFunction) {
  var asyncHanlder = {};
  var _callback = null;

  asyncFunction(function (data) {
    resolve(data);
  });

  function resolve(data) {
    _callback(data);
  }

  asyncHanlder.done = function (callback) {
    _callback = callback;
  }

  return asyncHanlder;
}

var asyncHanlder = asyncController(fetchData);
module.exports = asyncHanlder;

Wow! It sounds that this refactoring was really helpful. We create a function named asyncController that takes an async function parameter and returns an object named asyncHanlder which can be used to register a callback by its done method. We encapsulated the operation inside a function which causes to minimize the exposure. Also the App.js is still untouched.

Recommendation: Make sure you have completely understood until here, then go further.