admin管理员组文章数量:1326129
I have a promise/deferred set up for my web worker where the main thread makes a change in the web worker's data and tells me to recalculate. I have a promise to call it back upon pletion.
However, in mid process the main thread can make an additional change, again with the promise to call back upon pletion. (This interrupt can occur because the web worker makes a setTimeout call at times to allow interruptions.) At this point the web worker holds 2 promises to the main thread, both of which will return the exact same value.
Is it ok to throw away the first promise and only call back on the second. The code will all run fine and the main thread logic will be great (better in fact) with just the one call back. Is there any problem with doing this?
The promise is just a function stored in an object I'm holding in a hashtable. So if I remove it from the hashtable its memory should be returned to the heap and so there should be no leak of any kind.
So can I make my callback system a liar (when appropriate)?
I have a promise/deferred set up for my web worker where the main thread makes a change in the web worker's data and tells me to recalculate. I have a promise to call it back upon pletion.
However, in mid process the main thread can make an additional change, again with the promise to call back upon pletion. (This interrupt can occur because the web worker makes a setTimeout call at times to allow interruptions.) At this point the web worker holds 2 promises to the main thread, both of which will return the exact same value.
Is it ok to throw away the first promise and only call back on the second. The code will all run fine and the main thread logic will be great (better in fact) with just the one call back. Is there any problem with doing this?
The promise is just a function stored in an object I'm holding in a hashtable. So if I remove it from the hashtable its memory should be returned to the heap and so there should be no leak of any kind.
So can I make my callback system a liar (when appropriate)?
Share Improve this question asked Apr 24, 2014 at 19:07 David ThielenDavid Thielen 33k40 gold badges149 silver badges273 bronze badges 3- Yes, of course you can. Discarding previous results and waiting only for the last one is perfectly ok. It's even mon in cases like autoplete. You might want to consider better message passing if you support the cancellation promise API (lots of promise libraries do). – Benjamin Gruenbaum Commented Apr 24, 2014 at 19:14
- @BenjaminGruenbaum thank you. If you make that an answer I'm happy to accept it. Also, if you do, can you expand on "might want to consider better message passing". – David Thielen Commented Apr 24, 2014 at 19:18
- I'm still thinking about it more :) Also, this - github./petkaantonov/bluebird/blob/master/… – Benjamin Gruenbaum Commented Apr 24, 2014 at 19:19
1 Answer
Reset to default 6This is perfectly legitimate. As Ben points out in the ments, in cases like autoplete it's very mon. In fact, I wrote a library explicitly designed to support this kind of thing:
https://github./domenic/last
To quote from its readme:
var last = require("last"); var smartSearch = last(doSearch); $(searchEl).on("input", function (ev) { smartSearch(searchEl.value).then(updateUIWithResults).done(); });
The wrapped function will return a promise of the same type as that returned by the original. And once you call the wrapped function again, you will be guaranteed that previously-returned pending promises stay pending forever, so you don't have to worry about them ing back later than your new promise.
本文标签: Can I ignore a promise in javascriptStack Overflow
版权声明:本文标题:Can I ignore a promise in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742191783a2430350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论