最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

我该如何去“异步一路下滑”与节点Express吗?

IT培训 admin 3浏览 0评论

我该如何去“异步一路下滑”与节点Express吗?

我有很多异步功能在我的系统,所以我需要去“异步一路下跌”,这是要在其中创建的http.Serverexpress.Application应用点。

(这是一个异步系统不可避免的 - 将有哪些需要在构造函数中许多异步程序,这些程序无法做到的,因此我们需要使用异步工厂函数来代替,从而导致异步蠕变一路下跌到入口点。)

但我不知道该节点/打字稿语法的使用来引导的应用程序。

我的主要切入点是System.ts

class default export System {

  public constructor() {
    // init Express.Application
    // init http.Server
    // init other parts of the system
  }

  public async start(): Promise<void> {
    // start the system asynchronously
    // start listening with http.Server
  }

}

然后,我有一个引导模块Main.ts

import System from "./System"
const system = new System();
export default ???;                      // PROBLEM IS HERE

应运行:

node ./dist/Main.js

但我不知道在出口线路使用什么。我尝试了所有这些:

export default await system.start();     // doesn't compile (obviously)
export default system.start();           // doesn't seem right
export default system.start().then();    // this works *maybe*

最后一行的作品基于冒烟测试 - 但我不知道这是应该做的方式,以及是否有什么东西下来,可能会失败的行。

什么是启动一个异步节点应用的典型方式是什么?


UPDATE 根据@ JacobGillespie的回答,Main.ts引导模块现在是:

import System from "./System"
new System().start().then();
//new System().start().catch(e => console.error(e));  // alternative

就我而言,System.ts有错误和未处理的承诺处理程序,并不会记录(否则使用“另类”行)。因此,引导模块只是引导了系统。

回答如下:

这里async / await正在操作上的承诺,所以你基本上是要“开始”,通过调用.then.catch的承诺。

我去到内容片段在此是创造一个异步runmain功能,然后连接错误处理的过程中,这样的事情:

async function run() {
  // run the app, you can await stuff in here
}

run().catch(err => {
  console.error(err.stack)
  process.exit(1)
})

在你的情况,将看起来像(Main.ts):

import System from "./System"

async function run() {
  const system = new System()
  await system.start()
}

run().catch(err => {
  console.error(err.stack)
  process.exit(1)
})

你不需要因为这个模块文件不被进口其他地方(它的入口文件)出口任何东西。

你可以叫system.then()system.catch(),但我个人喜欢async function run()模式,因为你可能需要在未来协调多个异步的事情,这使得代码更明确。

我该如何去“异步一路下滑”与节点Express吗?

我有很多异步功能在我的系统,所以我需要去“异步一路下跌”,这是要在其中创建的http.Serverexpress.Application应用点。

(这是一个异步系统不可避免的 - 将有哪些需要在构造函数中许多异步程序,这些程序无法做到的,因此我们需要使用异步工厂函数来代替,从而导致异步蠕变一路下跌到入口点。)

但我不知道该节点/打字稿语法的使用来引导的应用程序。

我的主要切入点是System.ts

class default export System {

  public constructor() {
    // init Express.Application
    // init http.Server
    // init other parts of the system
  }

  public async start(): Promise<void> {
    // start the system asynchronously
    // start listening with http.Server
  }

}

然后,我有一个引导模块Main.ts

import System from "./System"
const system = new System();
export default ???;                      // PROBLEM IS HERE

应运行:

node ./dist/Main.js

但我不知道在出口线路使用什么。我尝试了所有这些:

export default await system.start();     // doesn't compile (obviously)
export default system.start();           // doesn't seem right
export default system.start().then();    // this works *maybe*

最后一行的作品基于冒烟测试 - 但我不知道这是应该做的方式,以及是否有什么东西下来,可能会失败的行。

什么是启动一个异步节点应用的典型方式是什么?


UPDATE 根据@ JacobGillespie的回答,Main.ts引导模块现在是:

import System from "./System"
new System().start().then();
//new System().start().catch(e => console.error(e));  // alternative

就我而言,System.ts有错误和未处理的承诺处理程序,并不会记录(否则使用“另类”行)。因此,引导模块只是引导了系统。

回答如下:

这里async / await正在操作上的承诺,所以你基本上是要“开始”,通过调用.then.catch的承诺。

我去到内容片段在此是创造一个异步runmain功能,然后连接错误处理的过程中,这样的事情:

async function run() {
  // run the app, you can await stuff in here
}

run().catch(err => {
  console.error(err.stack)
  process.exit(1)
})

在你的情况,将看起来像(Main.ts):

import System from "./System"

async function run() {
  const system = new System()
  await system.start()
}

run().catch(err => {
  console.error(err.stack)
  process.exit(1)
})

你不需要因为这个模块文件不被进口其他地方(它的入口文件)出口任何东西。

你可以叫system.then()system.catch(),但我个人喜欢async function run()模式,因为你可能需要在未来协调多个异步的事情,这使得代码更明确。

发布评论

评论列表 (0)

  1. 暂无评论