Reputation: 7591
My question is addition to: How to break out of jQuery each Loop
I need to exit from the jquery $.each() but in this case I cannot return FALSE, since the exit condition is firing a function that is going to resolve a defer. So I need to exit the each, but with a promise.
function KillLayer(id) {
var defer = $.Deferred();
$.each(vm.get("DeviceData"), function (idx, item) {
if (item.Type == id) {
vm.DeviceData.splice(idx, 1); // remove the item from the list
removeLayer(defer, id) // delete it from the PouchDB (IDB) database (async)
return defer.promise();
}
});
defer.resolve(); // layer was not found, so just resolve & return
return defer.promise();
}
Upvotes: 0
Views: 978
Reputation: 7591
I came up with Beetroots answer #1 independently. Firstly I realize that looping through an $each() and removing items from javascript array that your are looping is a great way for errors. I really should do something like this:
$.each(clone(vm.get("DeviceData")), function (idx, item)` {
where the clone function is defined as:
function clone(obj) {
return jQuery.extend({}, obj);
}
function deepClone(obj) {
return jQuery.extend(true, {}, obj);
}
But in my case, I don't need that extra protection, so I just updated my solution as follows:
function KillLayer(id) {
var defer = $.Deferred();
var found = false;
$.each(vm.get("DeviceData"), function (idx, item) {
if (item.Type == id) {
vm.DeviceData.splice(idx, 1);
Compare();
dbManifest.LayerInfo = vm.DeviceData.toJSON();
removeLayer(defer, id)
found = true;
return false;
}
});
if (!found) defer.resolve(); // layer was not found, so just resolve & return
return defer.promise();
}
Upvotes: 0
Reputation: 18078
As you have discovered, you can't return from KillLayer()
inside its each()
loop because a return statement in that context has a different meaning.
Here's a couple of different approaches :
1: Use a boolean flag to signal whether or not a deletion took place and treat the Deferred accordingly.
function KillLayer(id) {
var defer = $.Deferred(), deleted = false;
$.each(vm.get("DeviceData"), function (idx, item) {
if (item.Type == id) {
vm.DeviceData.splice(idx, 1); // remove the item from the list
removeLayer(defer, id); // delete it from the PouchDB (IDB) database (async)
deleted = true;
return false;//break from each() loop
}
});
return deleted ? defer.promise() : defer().resolve().promise();//return either a promise of deletion or a ready-resolved promise.
}
2: (If you have control over removeLayer()
) Arrange for removeLayer()
to generate and return its own promise, rather than passing in a Deferred. This will allow you to simplify KillLayer()
as follows:
function KillLayer(id) {
var deletionPromise;
$.each(vm.get("DeviceData"), function (idx, item) {
if (item.Type == id) {
vm.DeviceData.splice(idx, 1); // remove the item from the list
deletionPromise = removeLayer(id); // delete it from the PouchDB (IDB) database (async), and receive back a promise
return false;//break from each() loop
}
});
return deletionPromise || $.Deferred().resolve().promise(); //return either a promise of deletion from the DB, or a ready-resolved promise
}
Although there's only one line difference, I would contend that the second appraoch is cleaner. As removeLayer()
performs an asynchronous activity, it should really return a promise of its own, and not rely on a Deferred being passed to it.
Upvotes: 1
Reputation: 18068
This will not break from the each loop, but it will make the following iterations do nothing:
function KillLayer(id) {
var defer = $.Deferred();
var result = null;
$.each(vm.get("DeviceData"), function (idx, item) {
if (!result && item.Type == id) {
vm.DeviceData.splice(idx, 1); // remove the item from the list
removeLayer(defer, id) // delete it from the PouchDB (IDB) database (async)
result = defer.promise();
}
});
return result || defer.resolve();
}
Upvotes: 0
Reputation: 437
try like this.
objDP = null; // To get Deferred’s Promise object.
$.each(vm.get("DeviceData"), function (idx, item) {
if (item.Type == id) {
vm.DeviceData.splice(idx, 1); // remove the item from the list
removeLayer(defer, id) // delete it from the PouchDB (IDB) database (async)
objDP = defer.promise();
return false;
}
});
Upvotes: 1