text: import argsarray from 'argsarray';
  import { nextTick } from 'pouchdb-utils';
  
  var promisedCallback = function (promise, callback) {
    if (callback) {
      promise.then(function (res) {
        nextTick(function () {
          callback(null, res);
        });
      }, function (reason) {
        nextTick(function () {
          callback(reason);
        });
      });
    }
    return promise;
  };
  
  var callbackify = function (fun) {
    return argsarray(function (args) {
      var cb = args.pop();
      var promise = fun.apply(this, args);
      if (typeof cb === 'function') {
        promisedCallback(promise, cb);
      }
      return promise;
    });
  };
  
  // Promise finally util similar to Q.finally
  var fin = function (promise, finalPromiseFactory) {
    return promise.then(function (res) {
      return finalPromiseFactory().then(function () {
        return res;
      });
    }, function (reason) {
      return finalPromiseFactory().then(function () {
        throw reason;
      });
    });
  };
  
  var sequentialize = function (queue, promiseFactory) {
    return function () {
      var args = arguments;
      var that = this;
      return queue.add(function () {
        return promiseFactory.apply(that, args);
      });
    };
  };
  
  // uniq an array of strings, order not guaranteed
  // similar to underscore/lodash _.uniq
  var uniq = function (arr) {
    var map = {};
  
    for (var i = 0, len = arr.length; i < len; i++) {
      map['$' + arr[i]] = true;
    }
  
    var keys = Object.keys(map);
    var output = new Array(keys.length);
  
    for (i = 0, len = keys.length; i < len; i++) {
      output[i] = keys[i].substring(1);
    }
    return output;
  };
  
  export {
    uniq,
    sequentialize,
    fin,
    callbackify,
    promisedCallback
  };