Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

#287 Flush now returns promises correctly #294

Merged
merged 3 commits into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions index.js
Expand Up @@ -238,7 +238,8 @@ class Analytics {
callback = callback || noop

if (!this.enable) {
return setImmediate(callback)
setImmediate(callback)
return Promise.resolve()
}

if (this.timer) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
})
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions test.js
Expand Up @@ -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'
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Expand Up @@ -6011,11 +6011,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"
Expand Down