javascript - How to force a gulp task to wait until local function returns its result from a HTTP request? -


this shouldn't hard deal with, want regular callback method halt gulp task within executing, because http request inside of it.

gulp.task('html', function() { var params = {     apikey: '*****',     spaceid: '******',     opts:{         filter: '*****',         level: 3     } }; return $.remotesrc(languages, {     base: 'https://api',     qs: {         index: 'id',         fallback: 'en',         key: '****'     } })     .on('error', onerror) //here following function should wait result   secondfunction, avaible after gulp task finishes execution     .pipe($.foreach(function(stream, file) {         var data = json.parse(string(file.contents));         data.lang = file.path.match(/([^\/]+)(?=\.\w+$)/)[0];         params.locale = _.keys( _.pick(languagekeys, data.lang)) + _.values(_.pick(languagekeys, data.lang));        result = secondfunction(params);           return gulp.src(dist + '*.html')             .pipe($.template(data)).on('error', onerror)             .pipe(gulp.dest(dist + path.basename(file.path, '.json')));     })); });  var secondfunction = function (params) {     return  contentfuldata (params,  function (err, result) {         if(!err) {             //do             console.log('first'+ result);             // console.log(result);             return result;         }     
    }); }; 

stacktrace

the solution use promises , split task execution main task , dependency task, problem requests contentful not resolved before control passed on pipe($.template(data))

generating tempaltes before response came api, using array of promises resolved before merging content , generating templates solved it

    gulp.task('html',['contentful-faq'], function() {      promise.all(contentfulpromises).then(function (result) { //did result here                   }         }     });      return $.remotesrc(languages, {         base: 'https://api/path',         qs: {             index: 'id',             fallback: 'en',             key: 'abc123'         }     })         .on('error', onerror)         .pipe($.foreach(function(stream, file) {             var data = json.parse(string(file.contents));             data.lang = file.path.match(/([^\/]+)(?=\.\w+$)/)[0];              data.faq = _.merge(data.faq, contentfulresult[data.lang+languagekeys[data.lang]]);             return gulp.src(dist + '*.html')                 .pipe($.template(data))                 .on('error', onerror)                 .pipe($.if(isproduction, $.htmlmin({                     collapsewhitespace: true,                     conservativecollapse: true,                     removecomments: true,                     useshortdoctype: true,                     collapsebooleanattributes: true,                     removeredundantattributes: true,                     removeemptyattributes: true,                     minifyjs: true,                     minifycss: true                 })))                 .pipe(gulp.dest(dist + path.basename(file.path, '.json')));         })); });  //get faq contenful var contentfulpromises = []; gulp.task('contentful-faq', function (cb) {     var = this;     var params = {         apikey: 'xxxxxxxxx',         spaceid: 'abc123',         opts: {             filter: 'abc123',             level: 3,             order: 'fields.order'         }     };     var getcontenful = function (params) {         (var lang in languagekeys) {             params.locale = _.keys(_.pick(languagekeys, lang)) + _.values(_.pick(languagekeys, lang));             contentfulpromises.push(new promise(function (resolve, reject) {                 contentfuldata(params, function (err, result) {                     resolve(result);                 });             }));         }         promise.all(contentfulpromises)             .then(function (results) {             that.emit('end');         })             .then(function (err) {                 cb(err);             });     }(params); }).on('end', function (results) { }); 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -