javascript - Resolving or Rejecting a Promise while iterating through object properties -


this question has answer here:

i tried reject promise while iterating through object properties, execution continues after reject method called ("passed here!!!" logged in console, after rejection).

function updatedocumentwithnewdata(document, newdata) {   return new promise(function(resolve, reject) {      //some code...      (let key in newdata) {        if (newdata.hasownproperty(key)) {          //verifying if document has property          if (document.hasownproperty(key)) {            reject({'message' : 'a property exists...'});          }          //some code...        }      }                //some more code...      console.log("passed here!!!");      resolve(document);    });   }

i'm calling method returns promise this:

updatedocumentwithnewdata(doc, data).then(function(result) {    //some code    }).catch(function(err) {    //some code  });

the solution use boolean variable, , call "reject" method after loop finishes:

function updatedocumentwithnewdata(document, newdata) {    return new promise(function(resolve, reject) {      //some code...      let invalidupdate = false;      (let key in newdata) {        if (newdata.hasownproperty(key)) {          //verifying if document has property          if (document.hasownproperty(key)) {            invalidupdate = true;            break;          }          //some code...        }      }      if (invalidupdate) {        reject({'message' : 'a property exists...'});      }            //some more code...      console.log("passed here!!!");      resolve(document);    });   }

i don't know if i'm missing stupid, think rejection of promise should return imediatelly when "reject" called , break remaining code execution, first code should work. there i'm missing?

calling reject won't stop promise executing, it'll set promise's state rejected. doesn't make promise break code execution. (however, won't have problems calling resolve later, promise's state can changed pending rejected or fulfilled once)

if want break code execution, need use return reject(reason) (or return resolve(value)). otherwise, promise run until end of code, , then .then callbacks associated promise called. intended behavior. way stop promise execution throw error, cause promise reject error.

so way make original code work is:

function updatedocumentwithnewdata(document, newdata) {  return new promise(function(resolve, reject) {     //some code...     (let key in newdata) {       if (newdata.hasownproperty(key)) {         //verifying if document has property         if (document.hasownproperty(key)) {           return reject({'message' : 'a property exists...'});         }         //some code...       }     }      //some more code...     console.log("passed here!!!");     resolve(document);   });  } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -