javascript - Call object property from within a callback -


I have an async.series () function calling from another javascript object:

Main.js

var object 1 = Required ('./ object1'); Var object 1 = requirement ('./ object2'); Async.series ([object1.myfunction1, object2.anotherFunction]);

object1.js

  module.exports = {function1: function (callback) {async.each (someArray, function (Item, CB) {function2 (item);}, function (mistake) {if (mistake) return callback (mistake); callback ();}); }, Function 2: function (item, callback) {// something}};  

This code does not work because function2 is undefined within the callback. I have tried to put

  var refToFunction2 = this.function2  

at the beginning of function 1. It works if we call function 1 directly, but function 1 is called async and for some reasons: this = undefined.

Is there a clean way to do this?

You can set up your object1.js file as follows :

  var Async = require ('async'); Function function1 (callback) {async.each (someArray, function (item, cb) {function2 (item);}, function (mistake) {if (return) return callback (err); callback ();}); } Function Function 2 (item, callback) {// something} module Export Function1 = function1; Module.exports.function2 = function2;  

You should now note that function1 and function2 are defined globally in the file. This means that they can be said independently within each other.


Comments

Popular posts from this blog

mysql - How to enter php data into a html multiple select box -

java - Can't add JTree to JPanel of a JInternalFrame -

c++ - Cassandra datastax cpp driver - avoiding unnecessary copies -