Node.js 在节点启动期间,如何在主节点中等待?

Node.js 在节点启动期间,如何在主节点中等待?,node.js,Node.js,要从异步调用中初始化一个值,然后继续使用该值,我不知道在节点加载其他代码之前如何等待 console.log('---- initialize the value during startup ----'); const p1 = (async () => { const res = await requestAsync.get('https://nodejs.org/dist/latest-v8.x/docs/api/util.html'); return res.hea

要从异步调用中初始化一个值,然后继续使用该值,我不知道在节点加载其他代码之前如何等待

console.log('---- initialize the value during startup ----');
const p1 = (async () => {
    const res = await requestAsync.get('https://nodejs.org/dist/latest-v8.x/docs/api/util.html');
    return res.headers;
})();
const v2 = p1.then(v => (v));

console.log(v2);
console.log('---------- more work after v2 is resolved --------------');
我明白了

---- initialize the value during startup ----
Promise { <pending> }
---------- more work after v2 is resolved --------------

我不确定我是否理解你的意思,但也许这就是你想要的

async function main() {
  const response = await requestAsync.get('https://n...');
  const headers = response.headers;

  console.log('some other stuff here');
}

main();
将所有内容放在主函数中可以让您在执行任何其他操作之前等待初始请求


但是要小心:这是阻塞,因此如果请求需要一段时间,那么在完成之前,代码中不会发生任何事情。

我不确定我是否理解您的意思,但也许这就是您想要的

async function main() {
  const response = await requestAsync.get('https://n...');
  const headers = response.headers;

  console.log('some other stuff here');
}

main();
将所有内容放在主函数中可以让您在执行任何其他操作之前等待初始请求


但是要小心:这是阻塞,因此如果请求需要一段时间,那么在完成之前,代码中不会发生任何事情。

直到node.js具有当前没有的顶级wait,那么通常的处理方法就是使用。然后,将依赖于异步结果的所有内容放在处理程序中。然后:

如果只是出于代码结构原因,您可以将所有内容移动到函数中:

requestAsync.get('https://nodejs.org/dist/latest-v8.x/docs/api/util.html')
  .then(startup)
  .catch(handleErr);

function startup(result) {
   // put your startup code here that needs result
}

function handleErr(err) {
   // handle error here
}
请记住,任何其他启动代码都将继续,因为模块加载不会阻止等待此异步结果

如果要在一行中执行多个异步操作,则可以链接它们:

 requestAsync.get().then(f1).then(f2).catch(err => {....});
或者,您可以将代码包装在顶级函数中,以便使用wait:

 async function startup() {
      let result1 = await f1(...);
      let result2 = await f2(...);
      let result3 = await f3(...);
 }

 startup().then(finalResult => {
    // all async operation done here with finalResult
 }).catch(err => {
    // error starting up
 });

在node.js拥有当前没有的顶级wait之前,通常的做法是使用。然后,将依赖于异步结果的所有内容放在处理程序中。然后:

如果只是出于代码结构原因,您可以将所有内容移动到函数中:

requestAsync.get('https://nodejs.org/dist/latest-v8.x/docs/api/util.html')
  .then(startup)
  .catch(handleErr);

function startup(result) {
   // put your startup code here that needs result
}

function handleErr(err) {
   // handle error here
}
请记住,任何其他启动代码都将继续,因为模块加载不会阻止等待此异步结果

如果要在一行中执行多个异步操作,则可以链接它们:

 requestAsync.get().then(f1).then(f2).catch(err => {....});
或者,您可以将代码包装在顶级函数中,以便使用wait:

 async function startup() {
      let result1 = await f1(...);
      let result2 = await f2(...);
      let result3 = await f3(...);
 }

 startup().then(finalResult => {
    // all async operation done here with finalResult
 }).catch(err => {
    // error starting up
 });

打开iLife或包装所有代码,并将其正确链接,因为v2将在外部触发。不希望包装所有代码。当你有tuime的时候,你有一个完整的选择1的例子吗?打开IILife或者包装你所有的代码,并正确地链接它,因为如果v2在外面,它会在之前触发。不要包装所有的代码。当你有tuime时,你有完整的opt 1示例吗?初始化应该很快,所以等待启动是可以的。您的解决方案可能不起作用,因为如果我有另一个函数想要使用main的返回值,我可能会得到一个未初始化的返回值。main函数不返回任何内容,在它之外不应发生任何事情。如果您真的不想这样做,可以使用普通的承诺语法:requestAsync.get'https://nodejs/...'.thenresponse=>console.logresponsei c,我稍等了一下,看看是否还有其他方法初始化应该很快,所以在启动时等待是可以的。您的解决方案可能不起作用,因为如果我有另一个函数想要使用main的返回值,我可能会得到一个未初始化的返回值。main函数不返回任何内容,在它之外不应发生任何事情。如果您真的不想这样做,可以使用普通的承诺语法:requestAsync.get'https://nodejs/...’.thenresponse=>console.logresponsei c,我等了一会儿,看看是否还有其他方法