From 718993dd698e7235c650b2005b1e10695fa437e7 Mon Sep 17 00:00:00 2001 From: Dave Date: Fri, 3 Sep 2021 14:46:37 +1000 Subject: [PATCH] #287 Flush now returns promises correctly with the data that callback would have gotten. Removed pify from devDependencies --- index.js | 15 +++++++++++---- package.json | 1 - test.js | 2 -- yarn.lock | 5 ----- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 5dfe01c5..119f219e 100644 --- a/index.js +++ b/index.js @@ -238,7 +238,8 @@ class Analytics { callback = callback || noop if (!this.enable) { - return setImmediate(callback) + setImmediate(callback) + return Promise.resolve() } if (this.timer) { @@ -247,7 +248,8 @@ class Analytics { } if (!this.queue.length) { - return setImmediate(callback) + setImmediate(callback) + return Promise.resolve() } const items = this.queue.splice(0, this.flushAt) @@ -286,14 +288,19 @@ class Analytics { } return this.axiosInstance.post(`${this.host}${this.path}`, data, req) - .then(() => done()) + .then(() => { + done() + return Promise.resolve(data) + }) .catch(err => { if (err.response) { const error = new Error(err.response.statusText) - return done(error) + done(error) + throw error } done(err) + throw err }) } diff --git a/package.json b/package.json index 268166aa..4186985f 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,6 @@ "husky": "^3.0.4", "np": "^7.5.0", "nyc": "^15.1.0", - "pify": "^4.0.1", "sinon": "^7.3.2", "snyk": "^1.171.1", "standard": "^12.0.1" diff --git a/test.js b/test.js index 6bdd7992..ed274d50 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,6 @@ import bodyParser from 'body-parser' import express from 'express' import delay from 'delay' import auth from 'basic-auth' -import pify from 'pify' import test from 'ava' import Analytics from '.' import { version } from './package' @@ -28,7 +27,6 @@ const createClient = options => { }, options) const client = new Analytics('key', options) - client.flush = pify(client.flush.bind(client)) client.flushed = true return client diff --git a/yarn.lock b/yarn.lock index c4707bb9..08969783 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6005,11 +6005,6 @@ pify@^3.0.0: resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176" integrity sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY= -pify@^4.0.1: - version "4.0.1" - resolved "https://registry.yarnpkg.com/pify/-/pify-4.0.1.tgz#4b2cd25c50d598735c50292224fd8c6df41e3231" - integrity sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g== - pinkie-promise@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-1.0.0.tgz#d1da67f5482563bb7cf57f286ae2822ecfbf3670"