Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. What is the work of callback argument in NodeJs "module.exports"?

What is the work of callback argument in NodeJs "module.exports"?

Scheduled Pinned Locked Moved JavaScript
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MaxySpark
    wrote on last edited by
    #1

    I’m learning nodeJS. I don’t know what is the purpose of

    callback

    argument in

    module.exports

    . It pretty much look like

    return

    and

    console.log

    here is my code

    var request = require('request');

    module.exports = function(location,callback) {

    var encodedUrl = encodeURIComponent(location);
    //set url
    var url = "http://api.openweathermap.org/data/2.5/weather?q="+encodedUrl+"&APPID=575991c6b55a139d32139652d78bc8b8&units=metric";
    

    //location check
    if(!location) {
    callback("No location provided");
    } else {
    callback('location is '+location);
    }

    //request
    request({
    url: url,
    json: true
    }, function (error,response, body) {
    if(error) {
    callback('Unable To Fetch Weather');
    } else {
    //console.log(JSON.stringify(body, null, 4));
    callback("Current Temperature in "+body.name+" "+body.main.temp + '\xB0!!!');
    }
    });
    };

    R 1 Reply Last reply
    0
    • M MaxySpark

      I’m learning nodeJS. I don’t know what is the purpose of

      callback

      argument in

      module.exports

      . It pretty much look like

      return

      and

      console.log

      here is my code

      var request = require('request');

      module.exports = function(location,callback) {

      var encodedUrl = encodeURIComponent(location);
      //set url
      var url = "http://api.openweathermap.org/data/2.5/weather?q="+encodedUrl+"&APPID=575991c6b55a139d32139652d78bc8b8&units=metric";
      

      //location check
      if(!location) {
      callback("No location provided");
      } else {
      callback('location is '+location);
      }

      //request
      request({
      url: url,
      json: true
      }, function (error,response, body) {
      if(error) {
      callback('Unable To Fetch Weather');
      } else {
      //console.log(JSON.stringify(body, null, 4));
      callback("Current Temperature in "+body.name+" "+body.main.temp + '\xB0!!!');
      }
      });
      };

      R Offline
      R Offline
      rah_sin
      wrote on last edited by
      #2

      The purpose of callback is to invoke appropriate callback method based on error and success condition after calling third party api.

      rahul

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups