Resolve / reject do return role in javascript?

As mentioned in the title, the resolve and reject of a promise already "make role" of return or still yes (depending on the occasion) do I need to use return?

Explaining with code, I could do these two ways:

//Retorna os ids dos produtos da aplicação!
function getProducts() {
    return new Promise(function (resolve, reject) {
        fetch("/api/catalog_system/pub/products/search?fq=productId:" + dataLayer[0].productId).then(function (response) {
            return response.json()
        }).then(function (res) {
            if (!res || res.length <= 0) resolve(null)
            else resolve([dataLayer[0].productId, res[0]["Compre Junto"][0]])
        }).catch(function (error) {
            reject(error);
        })
    })
}

//Retorna os ids dos produtos da aplicação!
function getProducts() {
    return new Promise(function (resolve, reject) {
        fetch("/api/catalog_system/pub/products/search?fq=productId:" + dataLayer[0].productId).then(function (response) {
            return response.json()
        }).then(function (res) {
            if (!res || res.length <= 0) return resolve(null)
            else return resolve([dataLayer[0].productId, res[0]["Compre Junto"][0]])
        }).catch(function (error) {
            return reject(error);
        })
    })
}

Both work, though, I don't know if I should use return or not, if that would influence anything... Can anyone explain?

Thank you!

Author: Lucas de Carvalho, 2019-01-14

1 answers

The only difference is that when you use return in resolve / reject you inhibit the execution of the other codes below the resolve/reject call within the promises body.

See the example below:

function promises1(param){
  return new Promise(function(resolve, reject) {
      if(param){
         resolve('ok - promises1');
         console.log("execução continuada após resolver promises1");
      }else{
        reject('erro - promises1');
      }
  })
}

function promises2(param){
  return new Promise(function(resolve, reject) {
      if(param){
        return resolve('ok - promises2');
         
         console.log("execução não continuada após resolver promises2");
      }else{
        reject('erro - promises2');
      }
  })
}

 promises1(true)
   .then(function(mensagem){
      console.log(mensagem)
   })
   .catch(function(mensagem){
      console.log(mensagem)
   });
   
promises2(true)
   .then(function(mensagem){
      console.log(mensagem)
   })
   .catch(function(mensagem){
      console.log(mensagem)
   });
 1
Author: Marciano Machado, 2019-01-17 16:16:20