E
E
enchikiben2019-03-19 16:22:13
gulp.js
enchikiben, 2019-03-19 16:22:13

How to do asynchronous execution in gulp?

Good afternoon! It is necessary to start and wait for the task to be completed with bootprint, but it does not work with pipe how to do this, tell me?
I have such a task:

function generateDocsHtml() {
    return function (cb) {
        let bootprintService =
            bootprint
                .load(bootprint_swagger)
                .merge({
                    handlebars : {
                        partials : path.join(__dirname, partials),
                        templates : path.join(__dirname, templates),
                        helpers : path.join(__dirname, helpers),
                        data : {
                            items : items
                        }
                    },
                    less : {
                        main : [
                            path.join(__dirname, less + '/theme.less'),
                            path.join(__dirname, less + '/vars.less')
                        ]
                    }
                });

        return glob(build + '/**/*.json', function (err, files) {
            const items = [];
            for (let pathFile of files) {
                let fileName = pathFile.substring(pathFile.lastIndexOf('/') + 1);
                const savePath = path.join(__dirname, build);
                let name = fileName.split('.')[0].toLowerCase();
                items.push({
                    name : name,
                    fileName : fileName,
                });
            }

            items.forEach((item) => {
                const savePath = path.join(__dirname, build);
                bootprintService
                    .build(path.join(__dirname, build + '/' + item.name + '/' + item.name + '.json'), savePath + '/' + item.name)
                    .generate()
                    .done();
            });
        });
    }
}

the bottom line is that there can be a lot of json config files, I need to bypass them all and generate html for each, and only after that tell Gulpa that the task is completed, how can I do this? or do it differently

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2019-03-20
@EnChikiben

What else is return glob() ?
Everything is very simple - in done, call the task completion callback
Something like this

bootprintService
 …
 .done(function(){ cb() });

However, given that this crap is called in a cycle, each call should be wrapped in a promise, in done resolve the promise,
let services = [];
items.forEach((item) => {
  services.push(new Promise((resolve, reject)=>{
    bootprintService
    ...
    .done(() => resolve());
  }))
});

and call the callback at the end, after resolving all promises
Promise.all(services)
.then(function(){  cb() })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question