TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How do Promises Work?

219 pointsby themichaellaiover 9 years ago

16 comments

wonnageover 9 years ago
&gt; Another way of solving this problem comes from the realisation that we only really need to keep track of the dependencies for a promise while the promise is in the pending state, because once a promise is fulfilled we can just execute the function right away!<p>This is a common gotcha in Javascript implementations, in that you think you want this, but you really don&#x27;t! Now you never know if your code is synchronous or will run in a subsequent tick. Your call tree will look completely different depending on race conditions...<p>This comes up in user code as well; any time you write a function that takes a callback, it&#x27;s probably a good idea to either always run it either in the same call tree or in a new stack, but never mix the two. It&#x27;s usually easier to just do the latter using process.nextTick.
评论 #10616407 未加载
评论 #10616485 未加载
评论 #10617142 未加载
评论 #10617102 未加载
hvmonkover 9 years ago
I have seen how Promises&#x27; concept was abused in a project at work. All the promises were just returning Future objects, and they were exposed everywhere. And, in case a future fails for some reason, there was no way to have a new Future: all users were doing future.get, resulting in an exception thrown to the caller. What a mess.
评论 #10616437 未加载
评论 #10616230 未加载
评论 #10616105 未加载
radiospielover 9 years ago
Nicely done, thanks! Still, having to explain a concept as simple as eventual computation reinforces my belief that promises as a whole is broken, and should be done in something that looks like synchronous code with some help of the underlying runtime. (And no, ES7&#x27;s async is not good enough for me here)
评论 #10615758 未加载
评论 #10615814 未加载
评论 #10616363 未加载
评论 #10615784 未加载
hotBacteriaover 9 years ago
I found this document : <a href="https:&#x2F;&#x2F;github.com&#x2F;kriskowal&#x2F;q&#x2F;tree&#x2F;v1&#x2F;design" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kriskowal&#x2F;q&#x2F;tree&#x2F;v1&#x2F;design</a> very helpful to understand how promises work behind the scene Also liked being able to access the author&#x27;s reasoning and the motivations behind his design decisions
评论 #10617427 未加载
taternutsover 9 years ago
TIL: fulfil vs fulfill [1]<p>1. <a href="http:&#x2F;&#x2F;grammarist.com&#x2F;spelling&#x2F;fulfil-fulfill&#x2F;" rel="nofollow">http:&#x2F;&#x2F;grammarist.com&#x2F;spelling&#x2F;fulfil-fulfill&#x2F;</a>
评论 #10619070 未加载
inglorover 9 years ago
Another good classic about it is Domenic&#x27;s you&#x27;re missing the point of promises at <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;domenic&#x2F;3889970" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;domenic&#x2F;3889970</a> from 2012
elchiefover 9 years ago
Stupid Cisco blocked this site on my corp network, due to &quot;pornography&quot;, assuming due to &quot;lolita&quot; in URL.
评论 #10616494 未加载
sotojuanover 9 years ago
I can&#x27;t wait for async&#x2F;await enough myself.
评论 #10618906 未加载
benjaminjtover 9 years ago
Nice summary! I wish I had this when I started getting into Promises a couple of months ago.<p>For a really interesting look ahead to how Promises (and more!) might address sync&#x2F;async symmetry in ES6&#x2F;7, check this out: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;DqMFX91ToLw" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;DqMFX91ToLw</a>
ilakshover 9 years ago
Really a lot more explanation than necessary. Get a feel by using them and using a debugger or just console.log to trace execution. Then compare with equivalent callback code and async&#x2F;await with babel.<p>Then use async&#x2F;await and look at node-modules.com to find modules that convert to promises so you can use async&#x2F;await.
rdtscover 9 years ago
See section about handling error and promises. Very well done!<p>You won&#x27;t see that in the typical &quot;check out how cool promises are, async and fast all the things&quot;.<p>And promises indeed look cool, and make for nice short demos and they are easy to understand in short examples. Only when you start building a large applications based on them, where error handling has to be done, you start realizing they are bit like threads. Promise callback chains started from one event, can interfere with other promise callback chains that started from another event. And if they modify the same data, you now also have a race condition as well.<p>I would basically look at this statement &quot;In the synchronous world, it’s very simple to understand computations when thinking about functions: you put things into a function, and the function gives you something in return&quot; and follow through, but in a different direction -- pick the synchronous world if you can.<p>Sequential things should be sequential and concurrent things should be concurrent. Single request processing is sequential. For this request do x,y,z in order. Can&#x27;t do y unless x finished. Well sit and wait for x to finish. But requests themselves can be concurrent and run in parallel. For example a request comes in, reads the database, updates the database, bumps metrics, makess other sub-requests and then responds. It is sequential and should be kept sequential. If you platform cannot handle that, think very well about your platform, and perhaps pick a better platform. What does that mean practically? It means picking green threads (Python gevent, eventlet), it means Elixir, Erlang, Go goroutines, Rust&#x27;s threads. Streams can be used as a higher level abstraction sometimes and so on.
评论 #10617268 未加载
评论 #10618014 未加载
spooningtamarinover 9 years ago
They abide by the beautiful monadic laws.
评论 #10615759 未加载
debacleover 9 years ago
Maybe JavaScript isn&#x27;t the best language to explain Promises in considering how verbose they seem to be next to procedural code.
pjzedalisover 9 years ago
This article has put me over the ledge of taking some time to really study Go. Goroutines are starting to sound very attractive.
hoshover 9 years ago
And here I thought someone besides Mark Burgess was writing on Promise Theory...
anowlcalledjoshover 9 years ago
Came expecting psychology, was disappointed.