为什么Applisten在续集之前运行?

我认为代码在申请之前会成功返回,但它不像我想的那样工作。我不明白它是如何工作的? 某人能解释为什么吗?

sequelize
  .sync()
  .then(() => {
    console.log('successfull')
  })
  .catch(err => {console.error('err',err)});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use('/',loginRouter);
app.use('/admin',login);
app.use('/admin', indexRouter);  


app.listen(3001,()=>{console.log('App running at http://localhost:3001');
})
App running at http://localhost:3001
Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER NOT NULL auto_increment , `username` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `users`
successfull
评论
  • 安*陌离
    安*陌离 回复

    Its asynchronous... and not synchronouse. With an IIFE in combination with async / await you can let your code wait till your promise is settled up

    (async () => {
       await sequelize
         .sync()
         .then(() => {
           console.log('successfull')
         })
         .catch(err => {console.error('err',err)});
    })()