gpt4 book ai didi

javascript - 在 Node JS 中等待一个函数完成,然后再触发下一个函数

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:56 25 4
gpt4 key购买 nike

我有这个 gulp 任务:

// default task, runs through all primary tasks
gulp.task("default", ["media", "scripts", "styles", "html"], function () {
// notify that task is complete
gulp.src("gulpfile.js")
.pipe(plugins.gulpif(ran_tasks.length, plugins.notify({title: "Success!", message: ran_tasks.length + " task" + (ran_tasks.length > 1 ? "s" : "") + " complete! [" + ran_tasks.join(", ") + "]", onLast: true})));

// trigger FTP task if FTP flag is passed
if (plugins.argv.ftp) {
config_module.config(gulp, plugins, settings);
ftp_module.upload(gulp, plugins, settings, ran_tasks, on_error);
}

// reset ran_tasks array
ran_tasks.length = 0;
});

除了 FTP 位之外,效果很好。我需要先完成 config_module.config() ,然后才能触发 ftp_module.upload() 。我尝试过使用回调来设置 promise 和匿名函数,但是这些解决方案都不起作用; FTP 功能在配置之前不断触发。

如何让 ftp_module.upload() 函数在触发之前等待 config_module.config() 完成?

<小时/>

编辑:这是我尝试过的 promise ,但仍然无法正常工作:

new Promise(function (resolve) {
config_module.config(gulp, plugins, settings);
resolve();
}).then(function () {
ftp_module.upload(gulp, plugins, settings, ran_tasks, on_error);
});
<小时/>

编辑:我希望不必发布 modules_config.config() 代码,因为它很长,但我认为有必要继续:

module.exports = {
// config task, generate configuration file for FTP & BrowserSync and prompt dev for input
config(gulp, plugins, settings) {
// generate settings.json and start other functions
const generate_config = function (callback) {
return plugins.fs.stat("./settings.json", function (err) {
if (err !== null) {
const json_data =
`{
"ftp": {
"dev": {
"hostname": "",
"port": "21",
"mode": "ftp",
"username": "",
"password": "",
"path": ""
},
"dist": {
"hostname": "",
"port": "21",
"mode": "ftp",
"username": "",
"password": "",
"path": ""
}
},
"browsersync": {
"dev": {
"proxy": "",
"port": "",
"open": "",
"notify": ""
},
"dist": {
"proxy": "",
"port": "",
"open": "",
"notify": ""
}
}
}`;

plugins.fs.writeFile("./settings.json", json_data, "utf8", function () {
callback();
});
} else if (typeof callback === "function") {
callback();
}
});
};

// configue JSON data
const configure_json = function (namespace, options, env, callback) {
const prompts = [];

// construct the prompts
Object.keys(options).forEach(option => {
const properties = options[option];

// construct the prompt
const prompt = {
name: option,
message: namespace + " " + option + ": ",
};

// construct the prompt
Object.keys(properties).forEach(property => {
prompt[property] = properties[property];
});

// put the prompt in the array
prompts.push(prompt);
});

// prompt the user
return gulp.src("./settings.json")
.pipe(plugins.prompt.prompt(prompts, function (res) {
// open settings.json
const file = plugins.json.read("./settings.json");

// update data in JSON
Object.keys(options).forEach(option => {
file.set(namespace + "." + env + "." + option, res[option]);
settings[namespace][option] = res[option];
});

// write updated file contents
file.writeSync();

if (typeof callback === "function") {
callback();
}
}));
};

return new Promise (function (resolve) {
// get the target environment
const env = plugins.argv.dist ? "dist" : "dev";

// generate settings.json
generate_config(function () {
// read browsersync settings from settings.json
settings.browsersync.proxy = plugins.json.read("./settings.json").get("browsersync." + env + ".proxy");
settings.browsersync.port = plugins.json.read("./settings.json").get("browsersync." + env + ".port");
settings.browsersync.open = plugins.json.read("./settings.json").get("browsersync." + env + ".open");
settings.browsersync.notify = plugins.json.read("./settings.json").get("browsersync." + env + ".notify");

// read FTP settingss from settings.json
settings.ftp.host = plugins.json.read("./settings.json").get("ftp." + env + ".hostname");
settings.ftp.port = plugins.json.read("./settings.json").get("ftp." + env + ".port");
settings.ftp.mode = plugins.json.read("./settings.json").get("ftp." + env + ".mode");
settings.ftp.user = plugins.json.read("./settings.json").get("ftp." + env + ".username");
settings.ftp.pass = plugins.json.read("./settings.json").get("ftp." + env + ".password");
settings.ftp.path = plugins.json.read("./settings.json").get("ftp." + env + ".path");

// configure FTP credentials
configure_json("ftp", {
hostname: {
default: settings.ftp.host,
type: "input",
},
port: {
default: settings.ftp.port,
type: "input",
},
mode: {
default: settings.ftp.mode === "ftp" ? 0 : settings.ftp.mode === "tls" ? 1 : settings.ftp.mode === "sftp" ? 2 : 0,
type: "list",
choices: ["ftp", "tls", "sftp"],
},
username: {
default: settings.ftp.user,
type: "input",
},
password: {
default: settings.ftp.pass,
type: "password",
},
path: {
default: settings.ftp.path,
type: "input",
},
}, env, function () {
// configure BrowserSync settings
configure_json("browsersync", {
proxy: {
default: settings.browsersync.proxy === "" ? "localhost" : settings.browsersync.proxy,
type: "input",
},
port: {
default: settings.browsersync.port === "" ? "8080" : settings.browsersync.port,
type: "input",
},
open: {
default: settings.browsersync.open === "" ? "external" : settings.browsersync.open,
type: "input",
},
notify: {
default: settings.browsersync.open === "" ? "false" : settings.browsersync.open,
type: "input",
},
}, env, function () {
// resolve the promise
resolve();
});
});
});
});
}
};

如您所见,它正在返回一个 promise ,但无论出于何种原因,我仍然无法在其之后触发 FTP 任务。

最佳答案

您的问题已经有了一个可能的答案: promise 。

问题是你做错了。

在您在编辑中发布的代码(带有 promise )中,您正在调用 config_module 方法(这似乎是异步的),然后立即解决 promise 。在这种情况下,由于该方法是异步的,因此在 config 方法上的处理完成之前,promise 就得到了解决,从而导致了不需要的行为。

正确的方法是您应该 promise config_module 方法调用本身。这样,您只能在方法执行完全完成后“解决” promise 。

很难说 config_module 方法应该如何,因为您没有发布其代码。但是您应该在那里创建一个 promise ,然后仅在计算完成时才解决它。所以你可以这样:

config_module.config(gulp, plugins, settings)
.then(function () {
ftp_module.upload(gulp, plugins, settings, ran_tasks, on_error);
});

编辑:

发布 config_module 代码后,更容易看出唯一缺少的就是使用 config 方法返回的 Promise 在 .then block 内运行 ftp_module.upload

关于javascript - 在 Node JS 中等待一个函数完成,然后再触发下一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43400664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com