Skip to content

Commit d0e5da9

Browse files
authored
Merge pull request #138 from leapfrogtechnology/fix/return-from-callback
Fix: return response from callback
2 parents 39e49b7 + cd44aec commit d0e5da9

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

src/AsyncStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import AsyncStoreParams from './AsyncStoreParams';
44
* Async Store implementation contract.
55
*/
66
interface AsyncStore {
7-
initialize: (callback: (err?: any) => void, params?: AsyncStoreParams) => void;
7+
initialize: (callback: (err?: any) => void, params?: AsyncStoreParams) => any;
88
set: (properties: any) => void;
99
get: (key: string) => any;
1010
getAll: () => any;

src/impl/domain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function initialize(callback: (err?: any) => void, params?: AsyncStorePar
2828
d[STORE_KEY] = Object.create(null);
2929
d[ID_KEY] = randomUUID();
3030

31-
d.run(callback);
31+
return d.run(callback);
3232
}
3333

3434
/**

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function initializeFastifyPlugin(adapter: AsyncStoreAdapter = AsyncStoreA
9999
* Initialize the async store based on the adapter provided.
100100
*
101101
* @param {AsyncStoreAdapter} [adapter=AsyncStoreAdapter.DOMAIN]
102-
* @returns {(params: AsyncStoreParams) => void}
102+
* @returns {(callback: (err?: any) => void, params?: AsyncStoreParams) => any }
103103
*/
104104
export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN) {
105105
if (isInitialized()) {
@@ -111,7 +111,7 @@ export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN
111111
return (callback: (err?: any) => void, params?: AsyncStoreParams) => {
112112
initializedAdapter = adapter;
113113

114-
instance.initialize(callback, params);
114+
return instance.initialize(callback, params);
115115
};
116116
}
117117

test/domain.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,5 +839,88 @@ describe('store: [adapter=DOMAIN]', () => {
839839
globalStore.initialize(adapter)(callback);
840840
});
841841
});
842+
843+
it('should return the response from callback function.', (done) => {
844+
const callback = () => {
845+
globalStore.set({ foo: 'foo' });
846+
847+
return functionAccessingStore();
848+
};
849+
850+
const functionAccessingStore = () => {
851+
return globalStore.get('foo');
852+
};
853+
854+
const response = globalStore.initialize(adapter)(callback);
855+
expect(response).to.equal('foo');
856+
857+
done();
858+
});
859+
860+
it('should return the response from async callback function.', async () => {
861+
const callback = async () => {
862+
globalStore.set({ foo: 'foo' });
863+
864+
functionAccessingStore();
865+
const response = await asyncTask();
866+
867+
return response;
868+
};
869+
870+
const functionAccessingStore = () => {
871+
expect(globalStore.get('foo')).to.equal('foo');
872+
};
873+
874+
const asyncTask = () => {
875+
return new Promise((resolve) => {
876+
setTimeout(() => {
877+
resolve(globalStore.get('foo'));
878+
}, 1);
879+
});
880+
};
881+
882+
const response = await globalStore.initialize(adapter)(callback);
883+
expect(response).to.equal('foo');
884+
});
885+
});
886+
887+
describe('Error Handling:', () => {
888+
it('should bubble up the promise rejection from the callback.', async () => {
889+
const callback = () => {
890+
globalStore.set({ foo: 'foo' });
891+
892+
return new Promise((resolve, reject) => {
893+
setTimeout(() => {
894+
reject('Hello world');
895+
}, 1);
896+
});
897+
};
898+
899+
try {
900+
await globalStore.initialize(adapter)(callback);
901+
expect.fail('Should not reach here.');
902+
} catch (e) {
903+
expect(e).to.equal('Hello world');
904+
}
905+
});
906+
907+
it('should bubble up the error thrown from the callback.', (done) => {
908+
const callback = () => {
909+
globalStore.set({ foo: 'foo' });
910+
911+
throw new Error('Hello world');
912+
};
913+
914+
try {
915+
globalStore.initialize(adapter)(callback);
916+
expect.fail('Should not reach here.');
917+
} catch (e) {
918+
if (e instanceof Error) {
919+
expect(e.message).to.equal('Hello world');
920+
}
921+
}
922+
923+
done();
924+
});
842925
});
843926
});

0 commit comments

Comments
 (0)