Execute Program

JavaScript Concurrency: Omitting Promise Values

Welcome to the Omitting Promise Values lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • If a JavaScript function is written using { curly braces }, but doesn't have an explicit return statement, then it will always return undefined.

  • >
    function noReturn() {
    }
    noReturn();
    Result:
    undefinedPass Icon
  • The same goes for Promise.resolve: if we omit its argument, the promise will fulfill, but it will contain undefined.

  • >
    Promise.resolve();
    Asynchronous Icon Async Result:
    {fulfilled: undefined}Pass Icon
  • This applies to our then callbacks as well. If we don't return a value explicitly, the promise will fulfill with undefined.

  • >
    Promise.resolve(5)
    .then(function(n) {
    /* Do nothing. */
    });
    Asynchronous Icon Async Result:
    {fulfilled: undefined}Pass Icon
  • Here's a code problem:

    This promise is fulfilled with the value 5. Attach a then callback that doesn't return anything. That will create a promise that fulfills with undefined.

    Promise.resolve(5)
    .then(() => {
    });
    Asynchronous Icon Async Result:
    Goal:
    {fulfilled: undefined}
    Yours:
    {fulfilled: undefined}Pass Icon