Skip to content

Promise Adapter

Vitaly Tomilov edited this page Oct 14, 2017 · 17 revisions

PromiseAdapter class can take any promise library that doesn't implement a recognizable promise signature, and make it usable with pg-promise.

It is easier to understand what implementation an adapter requires, if we implement it for a promise library that's already fully compliant, and doesn't need an adapter, like ES6 Promise:

const pgpLib = require('pg-promise');

const adapter = new pgpLib.PromiseAdapter({
    create: cb => new Promise(cb),
    resolve: value => Promise.resolve(value),
    reject: reason => Promise.reject(reason),
    all: iterable => Promise.all(iterable)
});

const initOptions = {
    promiseLib: adapter
};

const pgp = pgpLib(initOptions);