Skip to content

Instantly share code, notes, and snippets.

@raganwald
Last active November 24, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raganwald/d768caadd208fcdf436e to your computer and use it in GitHub Desktop.
Save raganwald/d768caadd208fcdf436e to your computer and use it in GitHub Desktop.
Maybe I don’t understand promises, either.
// I expect:
doSomething().then(doSomethingElse())
.then(finalHandler);
// To act like:
/*
doSomething
|-----------------|
doSomethingElse(undefined)
|------------------------|
resultOfDoSomethingElse(resultOfDoSomething)
|------------------------------------------|
finalHandler(resultOfDoSomething)
|------------------|
*/
// or:
/*
doSomething
|-----------------|
doSomethingElse(undefined)
|--------------|
resultOfDoSomethingElse(resultOfDoSomething)
|------------------------------------------|
finalHandler(resultOfDoSomething)
|------------------|
*/
@raganwald
Copy link
Author

Note that my assumption is that doSomethingElse is a function that returns a function that returns a promise.

@BigAB
Copy link

BigAB commented Nov 24, 2015

You are not wrong.

Assuming of course:
doSomething returns a promise
resultOfDoSomethingElse also returns a promise, but for some reason required the value returned from doSomething

Here is an example of exactly that, playing out as you'd expect:
https://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=function%20deferred()%20%7B%0A%20%20%20var%20d%20%3D%20%7B%7D%3B%0A%20%20%20var%20p%20%3D%20new%20Promise(function(resolve%2C%20reject)%7B%0A%20%20%20%20%20%20%20d.resolve%20%3D%20resolve%3B%0A%20%20%20%20%20%20%20d.reject%20%3D%20reject%3B%0A%20%20%20%20%7D)%3B%0A%20%20%20d.promise%20%3D%20p%3B%0A%20%20%20return%20d%3B%0A%7D%0A%0Afunction%20doSomething(deferred)%20%7B%0A%20%20%20console.log(%22Do%20something%20called%22)%3B%0A%20%20%20return%20deferred.promise%3B%0A%7D%0A%0Afunction%20doSomethingElse()%20%7B%0A%20%20console.log(%22Do%20something%20else%20called%22)%3B%0A%20%20return%20function%20resultOfDoSomethingElse(value)%20%7B%0A%20%20%20%20var%20p%20%3D%20new%20Promise(function(resolve%2C%20reject)%7B%0A%20%20%20%20%20%20%20%20console.log(%22do%20something%20else%20resolves%22)%3B%0A%20%20%20%20%20%20%20%20setTimeout(()%3D%3E%7B%0A%20%20%20%20%20%20%20%20%20%20%20console.log(%22result%20of%20do%20something%20else%20resolves%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20resolve(value)%0A%20%20%20%20%20%20%20%20%7D%2C%203000)%3B%0A%20%20%20%20%7D)%3B%0A%20%20%20%20return%20p%3B%0A%20%20%7D%0A%7D%0A%0Afunction%20finalHandler(value)%7B%0A%20%20%20%20console.log(%22Final%20Handler%3A%22%2C%20value)%3B%0A%7D%0A%0A%2F*%20-----%20*%2F%0Alet%20d%20%3D%20deferred()%3B%0AdoSomething(d).then(doSomethingElse())%0A%20%20.then(finalHandler)%3B%0A%0Ad.resolve(%22I%20am%20the%20value%22)%3B%0A%0A%2F*%0AExpect%20to%20see%20the%20log%3A%0ADo%20something%20called%20%2F%2F%20immediately%0ADo%20something%20else%20called%20%2F%2Fimmediately%0A%0Ado%20something%20else%20resolves%20%2F%2F%20after%20d.resolve%20is%20called%0A%0Aresult%20of%20do%20something%20else%20resolves%20%2F%2F%20after%20a%20few%20seconds%0AFinal%20Handler%3A%20I%20am%20the%20value%20%2F%2F%20last%0A*%2F

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment