I'm trying to adapt IndexedDB to a promise, but the function checkUrlLink doesn't return any value. How can I fix this promise?
This is my code:
var promise = new Promise(function(resolve) {
return checkUrlLink(send_to_url, event);
}).then(function(url_link) {
console.log('URL LINK in promisse' + url_link);
return clients.openWindow(url_link);
});
function checkUrlLink(send_to_url, event) {
return new Promise((resolve,reject) => {
var request = indexedDB.open('db', 1);
var original_event = event;
request.onsuccess = function (event) {
//db = event.target.result;
var url_link = "url_link";
var store = request.result.transaction("uid", "readwrite").objectStore("uid");
var getRequest = store.get(url_link);
getRequest.onsuccess = function () {
var result = getRequest.result;
if (result) {
url_link = result;
//send_to_url(original_event,url_link);
resolve(url_link);
} else {
url_link_value = self.registration.scope;
store.add(url_link_value, url_link);
//send_to_url(original_event, url_link_value);
resolve(url_link_value);
}
};
};
request.onupgradeneeded = function (event) {
var db = event.target.result;
var store = db.createObjectStore('url_link');
};
});
}
I'm executing this inside a service worker.
You are using a Promise constructor anti-pattern here
var promise = new Promise(function(resolve) {
return checkUrlLink(send_to_url, event);
}).then(function(url_link) {
console.log('URL LINK in promisse' + url_link);
return clients.openWindow(url_link);
});
ie you're wrapping a function that returns a promise inside a new Promise
- there's no need for that. However, you never call resolve either, so that's two problems with that code
The above code is simply
var promise = checkUrlLink(send_to_url, event)
.then(function(url_link) {
console.log('URL LINK in promisse' + url_link);
return clients.openWindow(url_link);
};
now promise is a promise that will resolve to the value returned by clients.openWindow(url_link);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.