最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js function without callback - Stack Overflow

programmeradmin1浏览0评论

I have a node.js server. When a user requests a page I call a function that pulls some info from db and services the request. Simple function with callback then execute response.send

I need to perform secondary putation/database updates which are not necessary for rendering the page request. I don't want the user to wait for these secondary ops to plete (even though they take only 200 ms.)

Is there a way to call a function and exit gracefully without callback?

I have a node.js server. When a user requests a page I call a function that pulls some info from db and services the request. Simple function with callback then execute response.send

I need to perform secondary putation/database updates which are not necessary for rendering the page request. I don't want the user to wait for these secondary ops to plete (even though they take only 200 ms.)

Is there a way to call a function and exit gracefully without callback?

Share Improve this question edited Nov 17, 2014 at 4:43 royhowie 11.2k14 gold badges53 silver badges67 bronze badges asked Nov 12, 2014 at 21:41 user3658423user3658423 1,9445 gold badges30 silver badges51 bronze badges 1
  • 1 you can define an event and listen to this event – user2717954 Commented Nov 12, 2014 at 21:53
Add a ment  | 

6 Answers 6

Reset to default 3

You can simply do something like this

app.get('/path', function(req, res){
    getInfoFromDatabase();   // get info from the database
    res.render('myview', {info: data});

    // perform post render operations
    postRenderingCode();
    return;
});

If I understand your problem correctly you can use setTimeout with a value of 0 to place the maintenance code at the end of the execution queue.

function service(user, callback) {
  // This will be done later
  setTimeout(function() {
    console.log("Doing some maintenance work now...");
  }, 0);

  // Service the user
  callback("Here's your data " + user);
}

service("John", function(data) { console.log(data); });
service("Jane", function(data) { console.log(data); });

The output will be:

Here's your data John
Here's your data Jane
Doing some maintenance work now...
Doing some maintenance work now...

You can call your extra ASYNCHRONOUS function before, or after your actual response; for example:

yourCoolFunction() // does awesome stuff...

response.writeHead(200, 'OK');
response.write('some cool data response');
response.end();

Note that the "yourCoolFunction" mentioned must be asynchronous, else the rest of the code will wait for it to plete.

Assuming you're using express.js:

function(req, res, next) {
    doSomeAsyncWork(function(e, d) {
        // Some logic.
        doSomeMoreAsyncWork(function() {})
        res.send(/* some data*/)
    })
}

Basically you don't really care about the response of the additional async work so you can put in a function that does nothing for the callback.

since I can see none of the answers so far are even somehow helpful, and in order to avoid confusing. What I suggest is use on the object you are working on the following:

function doStuff() {
    myObj.emit('myEvent', param);
}

function callback(param) {
    do stuff;
}

myObj.on('myEvent', callback);

well, just do what you said, render the page, respond to the request and do whatever you have to do, your code isn't suddenly going to die because you responded to the request.

with express:

function handleTheRequest(req, res) {
    res.status(200).send("the response")
    // do whatever you like here
}
发布评论

评论列表(0)

  1. 暂无评论