承诺意外返回

时间:2017-06-04 23:35:44

标签: javascript promise bluebird

我有以下内容:

     return indexedDbClient.getStorageUsedInGb().then(function (storageUsedInGb) {
         var evictedMediaGuids = [];
         storageUsedInGb = parseFloat(storageUsedInGb);
         if (storageUsedInGb > storageQuotaInGb) {
            return new Promise(function(resolve, reject){
                const store = database.transaction(storeName, "readwrite").objectStore(storeName);
                (function loop(storageUsedInGb) {
                    if (storageUsedInGb <= storageQuotaInGb) {
                        resolve({
                            evictedMediaGuids: evictedMediaGuids,
                            shouldStopStoring: false
                        });
                    } else {
                        const latestMediaRequest = store.getAll();
                        latestMediaRequest.onsuccess = function (event) {
                            const allData = event.target.result;
                            const targetEntry = allData[0];
                            const deleteRequest = store.delete(targetEntry.media.guid);
                            evictedMediaGuids.push(targetEntry.media.guid);
                            deleteRequest.onsuccess = loop.bind(null, storageUsedInGb - event.target.media.size / 1024 / 1000 / 1000);
                            deleteRequest.onerror = reject;
                         }
                         latestMediaRequest.onerror = reject;
                    }
               })(storageUsedInGb); // call immediately
            }) 
         } else {
             return Promise.resolve({
                 evictedMediaGuids: evictedMediaGuids,
                 shouldStopStoring: false
             });
         }
        }).then(function (storeObject) {
            // do stuff to object
            return Promise.resolve(storeObject)
        });

这个想法是loop(storageUsedInGb)迫使决议等待return;但是handleStoreObject之后会立即调用loop - 没有调用latestMediaRequest onsuccess处理程序的迹象。我做错了什么?

我正在使用蓝鸟,以防万一。

0 个答案:

没有答案