You could use a separate initialise
method and call that from instance:(您可以使用单独的initialise
方法,并从实例调用该方法:)
class Server {
constructor(port) {
this.port = port;
}
initialise() {
return new Promise((resolve, reject) => {
const application = express();
const server = application.listen(this.port, function listening(error) {
if (error) reject(error);
resolve(server);
});
});
}
}
const server = new Server(port);
server.initialise().then(() => // do some stuff)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…