Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I ignore a promise in javascript?

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 completion.

However, in mid process the main thread can make an additional change, again with the promise to call back upon completion. (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)?

like image 560
David Thielen Avatar asked Apr 24 '14 19:04

David Thielen


People also ask

What happens when you reject a promise JavaScript?

If it rejects, it is rejected with the reason from the first promise that was rejected. Returns a new Promise object that is rejected with the given reason. Returns a new Promise object that is resolved with the given value.

Can we stop promise in JavaScript?

In modern JavaScript - no Promises have settled (hah) and it appears like it will never be possible to cancel a (pending) promise. Instead, there is a cross-platform (Node, Browsers etc) cancellation primitive as part of WHATWG (a standards body that also builds HTML) called AbortController .

What happens if you never resolve a promise?

A promise is just an object with properties in Javascript. There's no magic to it. So failing to resolve or reject a promise just fails to ever change the state from "pending" to anything else.

Can you reject a resolved promise?

A Promise executor should call only one resolve or one reject . Once one state is changed (pending => fulfilled or pending => rejected), that's all. Any further calls to resolve or reject will be ignored.


1 Answers

This is perfectly legitimate. As Ben points out in the comments, in cases like autocomplete it's very common. In fact, I wrote a library explicitly designed to support this kind of thing:

https://github.com/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 coming back later than your new promise.

like image 66
Domenic Avatar answered Oct 17 '22 23:10

Domenic