Node js callbacks in exported modules

user5569354

I'm a node newbie so forgive me if this is something simple.

I am trying to run a callback once a function has finished. No matter which way around I try it, the callback keeps executing first.

My module import_data.js

module.exports.download = function(url, path, supplier, callback) {
//SF add dates to logging
console.log('Import for '+supplier+' started');
request({uri: url})
    .pipe(fs.createWriteStream(path))
    .on('close', function() {
        console.log('Import complete');
    },function(err, data){
        callback;
    });
};

import_js is called into common

'use strict';

//var db              = require('../config/sequelize').sequelize;

var common              = require('./common/index.js'),
    async               = require('async');

common.importData(
    'www.url.com',
    '/tmp/target.csv', 
    'Target Compenents', 
    console.log('callback')
);

I would expect the console.log('calback') to be the last thing logged.

Any help greatly appreciated.

user5548116

You have to add a function as your callback, console.log it's only method it isn't a function which you can use as a callback.
So and to fix it you can wrap your console.log into function like below:

common.importData(
    'www.url.com',
    '/tmp/target.csv', 
    'Target Compenents', 
    function(){console.log('callback')}
);

i hope it will help you thanks!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Node js callbacks in exported modules

From Dev

No Exported Member / Node Modules

From Dev

@type for "exported modules" from node.js and good documentation descriptions?

From Dev

@type for "exported modules" from node.js and good documentation descriptions?

From Dev

What happens to modules required in exported modules Node?

From Dev

Node JS Callbacks

From Dev

'latLng' is not exported by 'node_modules/leaflet/dist/leaflet-src.js' Rollup build fails on leaflet

From Dev

Msal.js: node_modules/msal/lib-commonjs/index has no exported member 'User'

From Dev

Loops with callbacks in node.js

From Dev

Node JS Mongoose Async Callbacks

From Dev

Modules in Node.js

From Dev

Node.js - changing the value of an exported integer

From Dev

Working with exported functions in Node.js

From Dev

Node.js Control Flow and Callbacks

From Java

Replacing callbacks with promises in Node.js

From Dev

basic node.js callbacks assigned to variable

From Dev

Getting a better understanding of callbacks with Node.js

From Dev

"Call stack" for callbacks in node.js

From Dev

Waiting for multiple callbacks in Node.js

From Dev

Node.js promises, async, or just callbacks

From Dev

sqlite3 callbacks (node.js)

From Dev

How to create asynchronous callbacks in node js?

From Dev

"Call stack" for callbacks in node.js

From Dev

Problems with multiple Node.js callbacks

From Dev

Understanding node.js callbacks 2.0

From Dev

Node.js Express - next() callbacks

From Dev

understanding REALLY basic callbacks in node / js

From Dev

Node.js Control Flow and Callbacks

From Dev

Javascript / node.js - callbacks inside Promise