Promises VII: Encadeamento

As funções then() retornam uma nova promise, diferente da original. Como as funções catch são, na verdade, funções then() nos bastidores, elas também retornam novas promises. Então, se isso for verdade, você poderia fazer algo assim:

new Promise((resolve, reject) => {
    console.log("Initial");
    resolve();
})
.then(() => {
    throw new Error("Something failed");
    console.log("Do this");
})
.catch(() => {
    console.error("Do that");
})
.then(() => {
    console.log("Do this, no matter what happened before");
});

// logs
Initial
Do that
Do this, no matter what happened before

O texto "Do this" não é exibido porque o erro "Something failed" causou uma rejeição.

A última chamada de then() na função doSomething() deveria registrar in my main call something, mas registra undefined. Descubra o que há de errado com o código e corrija-o. Você verá dois registros in my function something; isso não é um erro. Isso vem do teste. Não remova nenhuma função then() ou catch().

Observações

  • Não complique demais!
  • Consulte a aba Resources se ficar travado.