2
var isError = false;
savedata.ingredients.split(',').reduce(function(p, ing) {
      return p.then(function() {
            return db.dog_ingredients.create({ content_name: ing, dogFoodId: dogId });
      });
}, Promise.resolve()).catch(function(e) {
      console.log(e);

      isError = true; ///// I want to change value at this point
});
console.log(isError); // result false. 
if(isError){
  res.status(400).send('Error');
}else{
  res.status(200).send('Good');
}

I think that external variable didn't change because promise is done asynchronously. but I don't know how to solve this problem.

Aram_
  • 101
  • 1
  • 3
  • 9

2 Answers2

3

I think that external variable didn't change because promise is done asynchronously.

Yes.

I don't know how to solve this problem.

Put the code that wants to evaluate the isError variable in a promise callback. In fact, better split it in the two then callbacks - one for the fulfilment and one for the rejection case, so that you don't need that boolean variable at all.

savedata.ingredients.split(',').reduce(function(p, ing) {
    return p.then(function() {
        return db.dog_ingredients.create({ content_name: ing, dogFoodId: dogId });
    });
}, Promise.resolve()).then(function() {
    res.status(200).send('Good');
}, function(e) {
    console.log(e);
    res.status(400).send('Error');
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • _"In fact, better"_ "better" is a subjective term, lacking a clear and unambiguous definition. – guest271314 Mar 06 '17 at 21:21
  • The point is "better" has nothing to do with program design either. The selection of an approach as to an application depends upon the expected result of the application and the environment. Every move in this universe has a moral component. Any person can express themselves however they please. That is Free Speech. That does not make any speech "better" than any other speech. It is all vibrations emitted into the universe. "better" could be omitted from Answer and still convey the concept on the merits of the written program itself. You presumed OP does not want to handle error, then proceed – guest271314 Mar 06 '17 at 21:32
  • The approaches at your Answer and http://stackoverflow.com/a/42634655/ are essentially equivalent as to the procedure and results of the program. – guest271314 Mar 06 '17 at 21:34
  • @guest271314 "Better" in the sense of any common objective code quality metric – Bergi Mar 06 '17 at 21:43
  • That is a false presumption. What is "code quality"? Is this codereview? There is no criteria for "code quality" at SO. Perhaps you should post a canonical Question/Answer as to "code quality", if that is on topic for SO. What do you mean by "common"? "common" as well, is subjective and not clearly defined? A group influenced by each other via peer pressure and convinced their point of view is "better"? "Not necessary" would have been an appropriate term to use if reason for "downvote" is returning resolved `Promise` from `.catch()`. – guest271314 Mar 06 '17 at 21:45
  • @guest271314 Of course there is. Bad solutions are downvoted, good solutions are upvoted. Of course there are no formal criteria, and many other things feed into votes, but that's the basic idea. – Bergi Mar 06 '17 at 21:49
  • We view the universe differently. "Bad" and "good" are subjective. Many have suffered and continue to suffer today for the "good" of the few at expense of the many. For the "better" the approach is "good". Objectively, the only difference between your approach and this users' approach is handling error in `.catch()` before returning a resolved `Promise`. That is objective. "Better", "good" and "bad" are emotional expressions, without relation to technical review of actual procedure. – guest271314 Mar 06 '17 at 21:52
  • @guest271314 Well, that's just how SO works. Everyone gets to vote, and who got the most subjective upvotes is elected "best". – Bergi Mar 06 '17 at 21:56
  • No, they are not "elected" "best". They may _feel_ or _believe_ that they are "best", though that is only in their own mind. "Elected" is for those whom are not free, not for the sovereign. – guest271314 Mar 06 '17 at 22:03
  • The "better" is still troubling, though that is this own users' issue. – guest271314 Mar 07 '17 at 01:02
1

The value is changed. But not changed yet when you log its value. There is no other way that waiting for the promise to be resolved or rejected to get the changed value.

ValLeNain
  • 2,232
  • 1
  • 18
  • 37