节点脚本等待异步回调

时间:2021-05-29 20:39:02

I have a simple js file I'm executing with node script.js that does some async stuff.

我有一个用节点脚本执行的简单js文件。做一些异步工作的js。

However, the script exits before the callbacks execute. I read that node "waits until callbacks are ran before exiting." This doesn't seem to be the case - what am I doing wrong?

但是,脚本在回调执行之前退出。我读取该节点“等待直到回调在退出之前运行”。这似乎不是事实——我做错了什么?

// script.js
asyncOp(function(err, result) { 
  console.log('callback'); // <- this doesn't happen 
}

Edit: Here is a concrete example. This is the entire script

编辑:这里有一个具体的例子。这是整个脚本。

// script.js
var User = require('../models/user'); // a mongoose model

User.create({name: "bob"}, function(err, user) { 
  console.log('callback'); // <- this doesn't happen 
};

I execute it with node script.js, but it exits before "callback" gets printed

我用节点脚本执行它。,但它在“回调”打印之前退出

1 个解决方案

#1


3  

looks like you need to connect to the server for the create() method to be executed mongoosejs.com/docs/models.html

看起来您需要连接到服务器,以便执行mongoosejs.com/docs/models.html的create()方法

#1


3  

looks like you need to connect to the server for the create() method to be executed mongoosejs.com/docs/models.html

看起来您需要连接到服务器,以便执行mongoosejs.com/docs/models.html的create()方法

相关文章