Giter Site home page Giter Site logo

"done" in the command chain about node-osmosis HOT 6 CLOSED

rchipka avatar rchipka commented on September 24, 2024
"done" in the command chain

from node-osmosis.

Comments (6)

rchipka avatar rchipka commented on September 24, 2024

Yes, done should be called after all callbacks and commands have called "next(context, data)".

The problem here is that then should come before the data command. The order of the console.log messages will then be: then, done, then data.

The reason data will still appear to happen last is because it's printing after an async function (fs.readdir) has been called. Unlike .then, .data doesn't expect a next(context, data) call. This means that Osmosis has no way of knowing when the .data callback finishes.

So done is the last thing being called, which means Osmosis is done doing everything it needs to and now your callback code is doing what it needs to.

If you merge your .data callback code with your .then callback code you can use next to tell Osmosis when your code is done. This will cause your console.log's to happen in the order you expect.

To verify the order:

var fs = require('fs');
var osmosis = require('osmosis');

osmosis
.get('www.craigslist.org/about/sites')
.data(function(data) {
    console.log('data');
})
.then(function(context, data, next) {
  console.log('then');
  next(context, data);
})
.done(function(){
  console.log('done');
})

from node-osmosis.

anasqadrei avatar anasqadrei commented on September 24, 2024

My real problem is something like this

osmosis
.get('www.craigslist.org/about/sites')
.then(function(context, data, next) {
  database.query(query, function(err, queryResults){
    //working on queryResults
    next(context, data);
  });
})
.done(function(){
  database.disconnect();
})

The problem is that it disconnects from the database before finishing the query which causes a database connection error in "then".

from node-osmosis.

rchipka avatar rchipka commented on September 24, 2024

In that case, done shouldn't be called until then has called next. Obviously this must not be the case if you are still having an issue. Is your "working on queryResults" code asynchronous? this would cause next to be called before it finished working on the query.

from node-osmosis.

anasqadrei avatar anasqadrei commented on September 24, 2024

Yes done shouldn't be called until then has called next. But that is not the case.

This is another illustration that proves that done is not waiting for next. I'm setting a timeout of 5 seconds and then call next.

var osmosis = require('osmosis');

osmosis
.get('www.google.com')
.then(function(context, data, next) {
    //call next after 5 secs
    setTimeout(function(){
      console.log('then');
      next(context, data);
    },5000);
})
.done(function(){
  console.log('done');
})

In that case, the output is done and 5 seconds later it writes then. Shouldn't done also wait 5 seconds for next to be called?

from node-osmosis.

rchipka avatar rchipka commented on September 24, 2024

There seems to be an issue when using then immediately before done. If you place a command between then and done then the code works as expected. This should be fixed in the next release.

In my test code I used the doc command in between:

var osmosis = require('/root/node-osmosis');

osmosis
.get('www.google.com')
.then(function(context, data, next) {
    //call next after 5 secs
    setTimeout(function(){
      console.log('then');
      next(context, data);
    },5000);
})
.doc()
.done(function(){
  console.log('done');
})

from node-osmosis.

rchipka avatar rchipka commented on September 24, 2024

As of Osmosis version 0.0.9 significant stack counting changes were made and these issues should be resolved.

from node-osmosis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.