3

I have read all the questions on SO about this subject but I'm still stuck because the condition function in promiseWhile does not take a parameter.

My use case is the following. I am trying to query some information for some date (start_date). I do not know whether there is info for start_date in my db so I want to check for that. If there is no data, I want to query the day before and keep doing that until there is data. (I know the promise while loop is not the best way to do that but I want to learn how to do it nonetheless)

Here is my code so far

let start_date = DateTime.fromFormat(req.body.date, "yyyy-MM-dd");
let date_promise = (the_date) => {
    let the_req = {
        date: the_date
    };
    return db.query(the_req);
};

let promiseWhile = Promise.method(function (condition, action) {
    if (!condition()) return;
    return action().then(promiseWhile.bind(null, condition, action));
});

promiseWhile(
    (body) => {return body.rows.length > 0},
    () => {
        start_date = start_date.minus(luxon.Duration.fromObject({days: 1}))
        return date_promise(start_date);
    },
).then((result) => {
    // start_date ... 
    // do something with the date I've obtained
});

date_promise return a promise.

In my promiseWhile condition I am trying to test that body.rows contains something with body being the parameter of the .then function after the result of date_promise resolves. (date_promise(some_date).then((body) => {...})).

I am not sure how to proceed from there. Any help welcome.

Chapo
  • 2,563
  • 3
  • 30
  • 60
  • 1
    First 2 problems I see is a) in your condition you say the function gets a `body` parameter, yet in `promiseWhile` you call the `condition` function with no parameters. b) on `action().then(promiseWhile(...))` you got the wrong syntax. Try changing it to `action().then(() => promiseWhile(...))` – Sagi Rika Jan 09 '20 at 08:10
  • Thks for taking the time. Yes a) exactly my issue. How do you write a `condition` function that takes a parameter ? – Chapo Jan 09 '20 at 08:14

1 Answers1

3

Promise.method is an older version of async functions. With that in mind and a few syntax corrections, your code would look like this:

let start_date = DateTime.fromFormat(req.body.date, "yyyy-MM-dd");

let date_promise = (the_date) => {
    let the_req = {
        date: the_date
    };
    return db.query(the_req);
};

const myAction = date => () => date_promise(date);

let promiseWhile = async function (condition, action) {
    const queryResults = await action();
    if (!condition(queryResults)) {
      start_date = start_date.minus(luxon.Duration.fromObject({days: 1}));
      return promiseWhile(condition, myAction(start_date));
    } else {
      return queryResults;
    }
};

promiseWhile(
    body => body.rows.length > 0,
    () => {
        return myAction(start_date);
    },
).then(result => { // The result you'll get here is queryResults.
    // start_date ... 
    // do something with the date I've obtained
});
Chapo
  • 2,563
  • 3
  • 30
  • 60
Sagi Rika
  • 2,839
  • 1
  • 12
  • 32
  • How do I get `body` (within `promiseWhile`) to be the result of the `date_promise().then(..)` ? – Chapo Jan 09 '20 at 08:15
  • I editted the question to recall promiseWhile with a new date every time. Please let me know how it goes. – Sagi Rika Jan 09 '20 at 08:28
  • trying now thank you for your time. Why have you created a `currentDate` variable instead of using `start_date` directly ? Also `myAction` is exactly `date_promise` isn't it ? Any reason why you created a new function ? – Chapo Jan 09 '20 at 09:12
  • 1
    OK it works. Great. Only things I modified was 1. I did not use the `currentDate` variable and 2. `myAction` shoudl return a function so I used `const myAction = (date) => { return () => date_promise(date); };` – Chapo Jan 09 '20 at 09:32
  • Could you modify your answer to incorporate those and I'll validate. Thanks again for your help. – Chapo Jan 09 '20 at 09:32
  • also the first call of promiseWhile should not modify `start_date` – Chapo Jan 09 '20 at 09:44
  • I modified my answer according to your comments. Please check it now. – Sagi Rika Jan 09 '20 at 10:34