【发布时间】:2020-08-06 00:32:35
【问题描述】:
我有节点程序,我需要在程序开头运行两个函数
稍后访问函数结果,当前每次等待每个函数都有效,
但是,为了节省时间而不是等待GetService 和GetProcess,因为我稍后需要项目中的数据
获取此数据大约需要 4 秒,我想在后台运行它,因为我不需要立即获得结果,
我如何在节点 js 中做到这一点,如果我运行 promise.all 它会等到 getService 和 getProcess 然后去程序的其余部分。
一个例子
function main() {
//I want to run this both function in background to save time
let service = await GetServices();
this.process = await GetProcess();
…..//Here additional code is running
//let say that after 30 second this code is called
Let users = GetUser(service);
Let users = GetAdress(this.process);
}
我实际上正在运行 yeoman 生成器 https://yeoman.io/authoring/ https://yeoman.io/authoring/user-interactions.html
export default class myGenerator extends Generator {
//here I want run those function in background to save time as the prompt to the user takes some time (lets say user have many questions...)
async initializing() {
let service = await GetServices();
this.process = await GetProcess();
}
async prompting() {
const answers = await this.prompt([
{
type: "input",
name: "name",
message: "Your project name",
default: this.appname // Default to current folder name
},
{
type: "confirm",
name: "list",
choises: this.process //here I need to data from the function running in background
}
]);
}
【问题讨论】:
标签: javascript node.js multithreading async-await