问题描述:

I'm trying to build a scraper using Node.js where I need some help implementing the callback pattern properly.

I'm having trouble implementing it the right what. What I am looking to do is something like this

var client = function(){};

client.prototype.init = function() {

// do something

}

client.prototype.A = function(callback) {

// do something

// run callback

if(recursive_condition_fulfilled)

myclient.A(callback)

}

var myClient = new client();

var myCallback = function(callback){

// do something

}

myClient.init();

myClient.A(myCallback);

The problem arises when I have an async function B() which runs inside A()

client.prototype.A = function(callback) {

// do something

this.B();

// wait for B

// once B is done

// run callback()

// then run the following

if(recursive_condition_fulfilled)

myclient.A(callback)

}

client.prototype.B = function(callback) {

// do something

// run callback

}

How do I implement the later part when B is resolved? (where it says)

// wait for B

// once B is done

// run callback()

相关阅读:
Top