-1

I am using async waterfall to process nested condition in request, and using express framework. But I am confused how to process data to the next function in waterfall while the data is <promise>. This promise data is a query from sequalize.

Here is the sketch

exports.getanythinghere = async function() {

  var query = "SELECT anything, here "
            + "FROM anywhere WHERE ignore this query";

  return new Promise((resolve, reject) => {
    db.sequelize.query(query , {
      type: QueryTypes.SELECT    
    }).then(wth => {
      resolve(wth);
    })
  });

}


async.waterfall([
  function(callback) {
    const trying = getanythinghere (); 
    callback(null, trying); 
  },
  function(dataone, callbackt) {
    console.log("dataone is ", dataone); 
  }
], function(err, res) {
  if (err) return callback(err);
  callback(null, res);    
});//waterfall

There dataone is always dataone is Promise { <pending> }

What I am missing here. In jquery, I will do getanythinghere().done(function(){});

But I want to have it in this callback of waterfall.

I used to do this few years ago, but I forgot since too much with java and php

Any help please..

Al Kasih
  • 887
  • 5
  • 24
  • 1
    "*I am using async waterfall*" - just don't, especially when using promises. Use a promise `.then()` chain or simply modern `async`/`await` syntax. – Bergi Dec 01 '21 at 20:06
  • Btw, avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it) in `getanythinghere`! – Bergi Dec 01 '21 at 20:07

1 Answers1

1

You don't need libraries like Async anymore, mainly because Javascript now supports async/await natively.

First, this is a Promise, because you can add it .then() :

db.sequelize.query(query , {type: QueryTypes.SELECT});

Since it is a Promise, you don't need to (and shouldn't) wrap it inside another Promise, and also you can simply await it, instead of using .then()

const trying = await db.sequelize.query(query , {type: QueryTypes.SELECT});

Using await or .then() resolves the promise, and you get a value, instead of Promise { <pending> }.

Then, you can directly use trying, you don't need to pass it to another function like a waterfall.

console.log("trying is ", trying);

Finally, all your code holds in four lines :

try{
  const query = `SELECT anything, here FROM anywhere WHERE ignore this query`;
  const trying = await db.sequelize.query(query , {type: QueryTypes.SELECT});
  console.log("trying is ", trying);
} catch(err) {
  console.log(err);
}
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • Thank you very much, I can't believe it can be so simple now. And it seems like I don't need waterfall anymore. – Al Kasih Dec 01 '21 at 20:29
  • But now I am worried about the real performance of js if using this `await` – Al Kasih Dec 01 '21 at 20:31
  • your answer lead me here. https://stackoverflow.com/questions/51519330/adding-async-function-to-async-waterfall – Al Kasih Dec 01 '21 at 20:39
  • Can you please check the method from `George T Kurian` is a really a good practice? It works very well. But it seems it doesnt return from callback anymore – Al Kasih Dec 01 '21 at 20:41
  • 1
    All this Async waterfall is just nonsense to me now. I used it several years ago, but since now JS supports `async/await` natively, why bother loading a whole library, constructing array of Promises with watrerfall callbacks and all? `async/await` is so simple, so lean and so powerful. I rewrote your 15 lines of code (and a library) into just 4 lines. If this does not convince you, I don't know what will :) – Jeremy Thille Dec 01 '21 at 20:54