Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How to simplify nested promises try catch?

I have the following function:

private bindSemanticObject(unom: number) {
    this.setSemanticService
        .bindObjectsSemanticByUnom(unom)
        .then(
            (addressSemantic) => {
                try {
                    this.setSemanticService.setAddressSemantic(addressSemantic);
                    this.setSemanticService.verifySemanticObject();
                    this.setSemanticService.setSemanticFields();
                    this.setSemanticService.saveSemantic().then(() => {
                        this.setSemanticService
                            .stateChangeBindObject()
                            .toPromise()
                            .then(() => {});

                        this.toastrService.success('Updated...');
                        this.editLayerFactory.destroy(false);
                    });
                } catch (e) {
                    console.log(e);
                }
            },
            (e) => {
                this.toastrService.warning(e);
            },
        )
        .catch((e) => console.log(e));
}

As you can notice this code has some .catch and one error block. How to simplify it and makre more readable?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here in 2022, you can markedly simplify that code using async/await, like this:

private async bindSemanticObject(unom: number) {
    try {
        const addressSemantic = await this.setSemanticService
                .bindObjectsSemanticByUnom(unom);
        this.setSemanticService.setAddressSemantic(addressSemantic);
        this.setSemanticService.verifySemanticObject();
        this.setSemanticService.setSemanticFields();
        await this.setSemanticService.saveSemantic();
        try {
            await this.setSemanticService
                .stateChangeBindObject()
                .toPromise();
        } catch {
            // You've said in a comment you don't care about
            // rejections on the part above
        }

        this.toastrService.success('Updated...');
        this.editLayerFactory.destroy(false);
    } catch (e) {
        this.toastrService.warning(e);
    }
}

A few notes on that:

  • You've said that you don't care about rejections from this.setSemanticService().stateChangeBindObject.toPromise(), so I've wrapped it in an inner try/catch.
  • You've also said that all (other) errors should be shown via toastr, even though the code in the question doesn't do that.
  • The outer try/catch will catch both synchronous errors thrown by the code and promise rejections (because of await).

If you can't use async/await for whatever reason, you can use a series of then handlers:

private bindSemanticObject(unom: number) {
    return this.setSemanticService
            .bindObjectsSemanticByUnom(unom)
            .then(addressSemantic => {
                this.setSemanticService.setAddressSemantic(addressSemantic);
                this.setSemanticService.verifySemanticObject();
                this.setSemanticService.setSemanticFields();
                return this.setSemanticService.saveSemantic();
            })
            .then(() => {
                return this.setSemanticService
                    .stateChangeBindObject()
                    .toPromise()
                    .catch(() => { /*...suppress...*/ });
            })
            .then(() => {
                this.toastrService.success('Updated...');
                this.editLayerFactory.destroy(false);
            })
            .catch(e => {
                this.toastrService.warning(e);
            });
}

That's not quite the same, it doesn't handle a synchronous error from the initial this.setSemanticService.bindObjectsSemanticByUnom(unom). If you need to handle that, change the beginning to:

private bindSemanticObject(unom: number) {
    return Promise.resolve().then(() =>
        this.setSemanticService
            .bindObjectsSemanticByUnom(unom)
    )
    .then(addressSemantic => {
        // ...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda