How to use the dexie.Promise function in dexie

To help you get started, we’ve selected a few dexie examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dfahlander / Dexie.js / test / tests-upgrading.js View on Github external
Dexie.delete("raw-db").then(function () {

        // Create a bare-bone indexedDB database with custom indexes of various kinds.
        return new Dexie.Promise(function (resolve, reject) {
            var indexedDB = Dexie.dependencies.indexedDB;
            var rawdb, req;

            function error(e) {
                if (rawdb) rawdb.close();
                reject(e.target.error);
            }

            req = indexedDB.open("raw-db", 2);
            req.onupgradeneeded = function (ev) {
                try {
                    console.log("onupgradeneeded called");
                    rawdb = req.result;
                    // Stores
                    var people = rawdb.createObjectStore("people", {keyPath: "_id", autoIncrement: false});
                    var messages = rawdb.createObjectStore("messages", {autoIncrement: true});
github dfahlander / Dexie.js / test / tests-exception-handling.js View on Github external
});

    // Also if the rejection was caused by a throw...
    new Dexie.Promise(function() {
        throw "second error (throw)";
    });

    // But when catched it should not trigger the global event:
    new Dexie.Promise(function(resolve, reject) {
        reject("third error (catched)");
    }).catch(function(e) {
        ok(true, "Catched error explicitely: " + e);
    });

    // If catching an explicit error type that was not thrown, it should be triggered
    new Dexie.Promise(function(resolve, reject) {
        reject("Simple error 1");
    }).catch(TypeError, function(e) {
        ok(false, "Error should not have been TypeError");
    });// Real error slip away... should be handled by global handler

    new Dexie.Promise(function(resolve, reject) {
        reject(new TypeError("Type Error 1"));
    }).catch(TypeError, function(e) {
        ok(true, "Catched the TypeError");
        // Now we have handled it. Not bubble to global handler!
    });

    Dexie.Promise.resolve(Promise.reject(new Error("Converting a rejected standard promise to Dexie.Promise but don't catch it")))
    .finally(()=>{    
        // With finally, it should yet trigger the global event:
        return new Dexie.Promise(function(resolve, reject) {
github dfahlander / Dexie.js / test / tests-transaction.js View on Github external
return db.transaction('rw', db.users, function () {
        return new Dexie.Promise(function (resolve, reject) {

            // Notify log when transaction completes too early
            Dexie.currentTransaction.complete(function () {
                ok(true, "Transaction committing too early...");
                // Resolve the promise after transaction commit.
                // Flow will continue in the same Transaction scope but with an
                // inactive transaction
                resolve();
            });

        }).then(function () {
            // Now when transaction has already committed, try to add a user with the current transaction:
            return db.users.add({ username: "arne" });
        }).then(function () {
            ok(false, "Should not be able to get a here transaction has become inactive");
        });
github dfahlander / Dexie.js / test / tests-promise.js View on Github external
spawnedTest("Dexie.Promise should act as being derived from global Promise", function* () {

    ok (Dexie.Promise !== Promise, "Just make sure global Promise isnt set to Dexie.Promise");
    
    let p = new Dexie.Promise(resolve => resolve());
    ok (p instanceof Promise, "new Dexie.Promise() instanceof window.Promise");

    //
    // Extend a static Promise method on global Promise:
    //
    Promise.myStaticMethod = function () {
        return "hello";
    }
    //
    // Extend a statefull method on global Promise:
    //
    Promise.prototype.myMethod = function (callback) {
        return "hola";
    }
        
    equal (Dexie.Promise.myStaticMethod(), "hello", "Could invoke a statically derived method");
github dfahlander / Dexie.js / test / tests-transaction.js View on Github external
await db.transaction('r', db.users, function () {
        // Since we do not return a promise here,
        // Promise.follow() will be used for awaitint all tasks.
        // However, tasks spawned under Dexie.ignoreTransacion() should not be included in promises to wait for.
        Dexie.ignoreTransaction(()=>{
            return new Dexie.Promise(resolve => setTimeout(resolve, 50)).then(()=>{
                return db.pets.put({kind: "dog"});
            }).then(()=>{
                return db.pets.count();
            }).then(numPets => {
                ok(true, `num pets: ${numPets}`);
                log.push("inner-task-done");
            }).then(resolve, reject);
        });
        // The following promise should be awaited for though (because new Promise is spawned from withing a zone or sub-zone to current transaction.)
        new Dexie.Promise(resolve => setTimeout(resolve, 25)).then(()=>{
            //return db.users.get(1);
        }).then(()=>{
            ok(true, "followed promise done");
            log.push("spawned-promise-done");
        }).catch(e => {
            ok(false, e);
        });
    });
github flattr / flattr-extension / src / lib / background / session / storage.js View on Github external
.then((status) =>
  {
    // Don't add attention if flattring is disabled
    if (status.combined == STATUS_BLOCKED ||
        status.combined == STATUS_DISABLED)
      return;

    let entity = getEntity(url);
    let property = (isManual) ? "manualAttention" : "attention";

    return new Dexie.Promise((resolve, reject) =>
    {
      db.transaction("rw", db.pages, function*()
      {
        let page = yield loadPage(url);
        let oldAttention = page.attention + page.manualAttention;
        page[property] += addedAttention;
        yield db.pages.update(page.url, {[property]: page[property]});

        if (addedAttention >= getRemainingAttention(url, oldAttention))
        {
          yield flattrManager.submit({
            entity, tabId, url,
            title: page.title,
            type: "attention"
          });
        }
github chrahunt / TagProReplays / src / js / modules / data.js View on Github external
function inner_loop(start) {
    logger.trace(`Executing inner loop on ${start}`);
    // Index of the end of this sequence of items.
    var n = Math.min(batch_size, total - start);
    // whether this is the last iteration.
    var last = start + batch_size >= total;
    return new Dexie.Promise(
    function inner_loop_promise(resolve, reject) {
      var dones = 0;
      var looped = false;
      function check(err) {
        // check looped to ensure that the table looping
        // is complete.
        // or is that redundant with checking n?
        if (dones === n && looped) {
          // reject only when looped?
          if (err) {
            reject(err);
          } else if (!last) {
            // recurse
            resolve(inner_loop(start + n));
          } else {
            resolve();
github dfahlander / Dexie.js / test / tests-promise.js View on Github external
.then (value => {
        return new Dexie.Promise(resolve => setTimeout(resolve, 0, "value"));
    }).then (value => {
        equal (value, "value", "When unresolved promise is resolved, this promise should resolve with its value");
github dfahlander / Dexie.js / test / tests-promise.js View on Github external
function createDirectlyResolvedPromise() {
    return new Dexie.Promise(function(resolve) {
        resolve();
    });
}
github dfahlander / Dexie.js / test / tests-promise.js View on Github external
new Dexie.Promise((resolve, reject) => {
        new Dexie.Promise((resolve2, reject2) => {
            reject2 ("error");
        }).then(resolve, e => {
            reject(e);
            //return Dexie.Promise.reject(e);
        });
    });