This question has already been answered:

Tell me why this code does not display any errors, and does not display anything in the console.

Trying to do a consistent execution of functions.
I wonder why this particular code does not work?
In the debugger, it is not even included in the runF() function.

 function Queue() { Queue.prototype.constructor = function() {}; this._this = this; this.funccollect = []; } Queue.prototype.addFunc = function(callback) { this.funccollect.push(callback); return this; } Queue.prototype.runF = function() { this.funccollect[1].apply(this, arguments); } var queue = new Queue(); queue.addFunc(function() { setTimeout(function() { console.log('Первая функция'); queue.runF(); }, 2000); }) .addFunc(function() { console.log('Вторая функция'); }); 

Reported as a duplicate at Grundy. javascript May 8 '18 at 15:49 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    3 answers 3

    Well, where is the call runF ?

     function Queue() { Queue.prototype.constructor = function () {}; this._this = this; this.funccollect= []; } Queue.prototype.addFunc = function (callback){ this.funccollect.push(callback); return this; } Queue.prototype.runF = function () { this.funccollect[1].apply(this,arguments); } var queue = new Queue(); queue.addFunc(function () { setTimeout(function (){ console.log('Первая функция'); queue.runF(); },2000); }) .addFunc(function () { console.log('Вторая функция'); }); queue.runF(); 

    • Understand the error, thanks. - user3035145
    • this call was inside setTimeout . And in your decision there is still no output 'Первая функция' - Grundy
    • @Grundy "In debugger, it is not even included in the runF () function." - in my decision - enters. - Igor
    • Included :-) but the runF call runF already there, but the first function is not executed :) - Grundy
    • funccollect[0] forgotten funccollect[0] - vp_arth

    Such things are best done on the basis of Promise .
    Sketched a variation with the support of old-fashioned (err, res) callbacks.

     class Queue { constructor() { this.list = []; } pushWithCb(func) { // func(prev, next) where next(err, result) this.push((...args) => { return new Promise((ok, fail) => { args.push((err, res)=>{ if (err) return fail(err); ok(res); }); func(...args); }); }); return this; } push(func) { this.list.push(func); return this; } run() { let p = Promise.resolve(); this.list.forEach((func) => { p = p.then(func); }); return p; } } let q = new Queue(); q.pushWithCb(function(_, next) { next(null, 1); }).push(function(res1) { return Promise.resolve(res1 + 1); }).push(function(res2) { return res2 + 1; }).run() .then(res => console.log(res)) .catch(error => console.error(error)); 

      Who is interesting to do like this.

       function Queue() { this.index= 0; Queue.prototype.constructor = function () {}; this._this = this; this.defferent = []; } Queue.prototype.addFunc = function (callback){ this.defferent.push(callback); return this; } Queue.prototype.runF = function () { this.defferent[this.index].apply(this,arguments); this.index++; } var queue = new Queue(); queue.addFunc(function () { setTimeout(function () { console.log('Первая функция'); queue.runF();},2000); }) .addFunc(function () { console.log('Вторая функция'); } ).runF(); 
      • In other words, did you use the @Igor solution? - user236014
      • @Anon, not really, then the essence of the index appears - vp_arth