Skip to content

Instantly share code, notes, and snippets.

@xandrw
Forked from nolanlawson/promises_answer_sheet.md
Created June 23, 2016 22:20
Show Gist options
  • Save xandrw/b64130ff6327459e97e9c296ca57c418 to your computer and use it in GitHub Desktop.
Save xandrw/b64130ff6327459e97e9c296ca57c418 to your computer and use it in GitHub Desktop.
Promises puzzle cheat sheet

Promises puzzle answer sheet

Solution to https://twitter.com/nolanlawson/status/578948854411878400.

Puzzle #1

doSomething().then(function () {
  return doSomethingElse();
}).then(finalHandler);

Answer:

doSomething
|-----------------|
                  doSomethingElse(undefined)
                  |------------------|
                                     finalHandler(resultOfDoSomethingElse)
                                     |------------------|

Puzzle #2

doSomething().then(function () {
  doSomethingElse();
}).then(finalHandler);

Answer:

doSomething
|-----------------|
                  doSomethingElse(undefined)
                  |------------------|
                  finalHandler(undefined)
                  |------------------|

Puzzle #3

doSomething().then(doSomethingElse())
  .then(finalHandler);

Answer:

doSomething
|-----------------|
doSomethingElse(undefined)
|---------------------------------|
                  finalHandler(resultOfDoSomething)
                  |------------------|

Puzzle #4

doSomething().then(doSomethingElse)
  .then(finalHandler);

Answer:

doSomething
|-----------------|
                  doSomethingElse(resultOfDoSomething)
                  |------------------|
                                     finalHandler(resultOfDoSomethingElse)
                                     |------------------|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment