From a3513185ec99e4e4b5f657ad784157a7293eff45 Mon Sep 17 00:00:00 2001 From: Eli Bishop Date: Wed, 20 Nov 2019 11:20:09 -0800 Subject: [PATCH] Node 6 compatibility --- test/async_utils.js | 11 +++++++++++ test/file_data_source-test.js | 3 +-- test/polling-test.js | 3 +-- test/requestor-test.js | 3 +-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/test/async_utils.js b/test/async_utils.js index b1db260..a265ed2 100644 --- a/test/async_utils.js +++ b/test/async_utils.js @@ -1,4 +1,14 @@ +// Converts a function that takes a Node-style callback (err, result) as its last argument into a +// function that returns a Promise. This is equivalent to util.promisify, but is reimplemented here +// because promisify isn't supported in Node 6. +// Usage: await asyncifyNode(doSomething)(allParamsExceptCallback) +function promisify(f) { + return (...args) => + new Promise((resolve, reject) => + f(...args, (err, result) => err ? reject(err) : resolve(result))); +} + // Converts a function that takes a single-parameter callback (like most SDK methods) into a Promise. // This is different from util.promisify, which uses Node-style callbacks with two parameters. // Usage: await asyncify(callback => doSomething(params, callback)) @@ -83,6 +93,7 @@ function AsyncQueue() { module.exports = { asyncify, + promisify, sleepAsync, withCloseable, AsyncQueue, diff --git a/test/file_data_source-test.js b/test/file_data_source-test.js index db80139..2ef4c82 100644 --- a/test/file_data_source-test.js +++ b/test/file_data_source-test.js @@ -1,8 +1,7 @@ const fs = require('fs'); const tmp = require('tmp'); -const { promisify } = require('util'); const dataKind = require('../versioned_data_kind'); -const { asyncify, sleepAsync } = require('./async_utils'); +const { asyncify, promisify, sleepAsync } = require('./async_utils'); const { stubLogger } = require('./stubs'); const LaunchDarkly = require('../index'); diff --git a/test/polling-test.js b/test/polling-test.js index 2cdf771..e296f86 100644 --- a/test/polling-test.js +++ b/test/polling-test.js @@ -1,8 +1,7 @@ -const { promisify } = require('util'); const InMemoryFeatureStore = require('../feature_store'); const PollingProcessor = require('../polling'); const dataKind = require('../versioned_data_kind'); -const { asyncify, sleepAsync } = require('./async_utils'); +const { asyncify, promisify, sleepAsync } = require('./async_utils'); const stubs = require('./stubs'); describe('PollingProcessor', () => { diff --git a/test/requestor-test.js b/test/requestor-test.js index 169e7bb..7d6c551 100644 --- a/test/requestor-test.js +++ b/test/requestor-test.js @@ -1,7 +1,6 @@ -import { promisify } from 'util'; import Requestor from '../requestor'; import * as dataKind from '../versioned_data_kind'; -import { withCloseable } from './async_utils'; +import { promisify, withCloseable } from './async_utils'; import { createServer, respond, respondJson } from './http_server'; describe('Requestor', () => {