Gulpjs wait for async function before continue

I cut gulpfile.js into several files. My purpose is to share the data with various tasks. And I use windows system, gulp 5 and “type”: “module” config.

The file structure is given below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>gulpfile.js
tasks/clean.js
tasks/deploy_data.js
data/path.js
data/other.js
</code>
<code>gulpfile.js tasks/clean.js tasks/deploy_data.js data/path.js data/other.js </code>
gulpfile.js
tasks/clean.js
tasks/deploy_data.js
data/path.js
data/other.js

gulpfile.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import gulp from 'gulp';
import clean from './tasks/clean.js';
import deploy_data from './data/deploy_data.js';
global.data = {}; // share data with all tasks.
function deploy_at_start() {
gulp.series(deploy_data);
}
deploy_at_start(); // I hope that deploy_data will be executed before any task I debug, because each task needs to use the contents of data.
export {
clean,
// alot of other tasks
};
</code>
<code>import gulp from 'gulp'; import clean from './tasks/clean.js'; import deploy_data from './data/deploy_data.js'; global.data = {}; // share data with all tasks. function deploy_at_start() { gulp.series(deploy_data); } deploy_at_start(); // I hope that deploy_data will be executed before any task I debug, because each task needs to use the contents of data. export { clean, // alot of other tasks }; </code>
import gulp from 'gulp';
import clean from './tasks/clean.js';
import deploy_data from './data/deploy_data.js';

global.data = {}; // share data with all tasks.

function deploy_at_start() {
  gulp.series(deploy_data);
}

deploy_at_start(); // I hope that deploy_data will be executed before any task I debug, because each task needs to use the contents of data.

export {
  clean,
  // alot of other tasks
};

./tasks/clean.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function clean(cb) {
console.log(data.path.to.del); // I need to get some information from data, such as the path I want to delete.
cb();
}
export default clean;
</code>
<code>function clean(cb) { console.log(data.path.to.del); // I need to get some information from data, such as the path I want to delete. cb(); } export default clean; </code>
function clean(cb) {
  console.log(data.path.to.del); // I need to get some information from data, such as the path I want to delete.
  cb();
}

export default clean;

The point is this

data/path.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const path = [
{
name: 'del',
src: './folder/*',
},
];
export default path;
</code>
<code>const path = [ { name: 'del', src: './folder/*', }, ]; export default path; </code>
const path = [
  {
    name: 'del',
    src: './folder/*',
  },
];

export default path;

tasks/deploy_data.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import path from 'node:path';
import { pathToFileURL } from 'node:url';
async function deploy_data(cb) {
const dir = path.join(process.cwd(), 'data');
const files = fs
.readdirSync(dir, { withFileTypes: true }).filter((item) => item.isFile())
.map((item) => item.name); // I have a lot of data files, so this is the easier way to get all the data
for (const file of files) {
data[path.parse(file).name] = (await import(pathToFileURL(path.join(dir, file)).href)).default;
}
cb();
}
export default deploy_data;
</code>
<code>import path from 'node:path'; import { pathToFileURL } from 'node:url'; async function deploy_data(cb) { const dir = path.join(process.cwd(), 'data'); const files = fs .readdirSync(dir, { withFileTypes: true }).filter((item) => item.isFile()) .map((item) => item.name); // I have a lot of data files, so this is the easier way to get all the data for (const file of files) { data[path.parse(file).name] = (await import(pathToFileURL(path.join(dir, file)).href)).default; } cb(); } export default deploy_data; </code>
import path from 'node:path';
import { pathToFileURL } from 'node:url';

async function deploy_data(cb) {
  const dir = path.join(process.cwd(), 'data');
  const files = fs
.readdirSync(dir, { withFileTypes: true }).filter((item) => item.isFile())
.map((item) => item.name); // I have a lot of data files, so this is the easier way to get all the data

  for (const file of files) {
    data[path.parse(file).name] = (await import(pathToFileURL(path.join(dir, file)).href)).default;
  }

  cb();
}

export default deploy_data;

When I execute gulp clean, I can’t get the data.path information. I think it may be because deploy_data is async and something went wrong.

From the terminal, I can see that deploy_data completes its work after clean is completed, which is why data is empty.

I understand that I can use gulp.series to execute deploy_data before clean, but I have a huge amount of data. I think I can achieve the goal by executing deploy_data once at the beginning of gulpfile.js similar to the current method.

In the above case, is there a way for me to execute deploy_data before executing any gulp task (the task that has been exported) and wait for it to complete before executing other code?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật