Reputation: 1075
I have the following code:
class Request {
constructor(method, url) {
this.method = method;
this.url = url;
}
send() {
return fetch(this.url, { method: this.method })
.then((res) => res.json());
}
}
const url = "https://ron-swanson-quotes.herokuapp.com/v2/quotes";
const getQuotes = new Request("get", url);
const all = {
getQuotes
};
getQuotes.send().then(alert);
delete all.getQuotes;
Can somebody, please, explain why does getQuotes.send()
resolve even after I have explicitly deleted a class instance on which the promise was executed.
Upvotes: 4
Views: 65
Reputation: 92735
Because you run promise by .then((res) => res.json());
(inside send()
, and also further by .then(alert)
). By the way: when you execute code
const getQuotes = new Request("get", url);
const all = { getQuotes };
then you request object is in two places: in getQuotes
constant, and in all.getQuotes
object field. So when you executed delete all.getQuotes
, the getQuotes
constant is still not empty (but this is not reason of promise execution).
Upvotes: 0
Reputation: 7241
You have deleted the reference to the Request
instance from the all
object. I.e. removed the getQuotes
property. The getQuotes
const defined previously is still there and the promise is still running.
Upvotes: 3