Skip to content

Dexie.async()

David Fahlander edited this page Mar 15, 2016 · 15 revisions

Syntax

var myAsyncFunction = Dexie.async(function* () {
    // Function body goes here.
    // To await, use the yield keyword.
});

Return Value

Promise

Description

Makes it possible to use async functions with modern browsers (Chrome, Firefox, Opera and Edge) without the need of a transpiler.

Table below shows how this maps to ES7 async / await.

                           +--------------------------------+--------------------------+
                           | Using function*() and yield    | Using async / await      |
+--------------------------+--------------------------------+--------------------------+
| Declare async function   | Dexie.async(function* () {});  | async function() {}      |
+--------------------------+--------------------------------+--------------------------+
| Declare+execute function | Dexie.spawn(function* () {});  | (async function() {})()  |
+--------------------------+--------------------------------+--------------------------+
| await a Promise          | yield p;                       | await p;                 |
+--------------------------+--------------------------------+--------------------------+
| Declare Promise Generator| function* f (){}               | N/A                      |
+--------------------------+-----------------------------------------------------------+
| await Promise Generator  | yield* f();                    | N/A                      |
+--------------------------+-----------------------------------------------------------+

Sample

db.delete().then(function() {
    console.log("Database successfully deleted");
}).catch(function (err) {
    console.error("Could not delete database");
}).finally(function() {
    // Do what should be done next...
});
Clone this wiki locally