Skip to content

Commit

Permalink
Converted some stray promise chains to async/await (#2697)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz authored May 5, 2020
1 parent 0fbc5b9 commit e0e3e30
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/eighty-boxes-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/keystone': patch
'@keystonejs/utils': patch
---

Converted some stray promise chains to async/await.
21 changes: 7 additions & 14 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,27 +453,20 @@ module.exports = class Keystone {
/**
* @return Promise<null>
*/
connect() {
async connect() {
const { adapters, name } = this;
const rels = this._consolidateRelationships();
return resolveAllKeys(mapKeys(adapters, adapter => adapter.connect({ name, rels }))).then(
() => {
if (this.eventHandlers.onConnect) {
return this.eventHandlers.onConnect(this);
}
}
);
await resolveAllKeys(mapKeys(adapters, adapter => adapter.connect({ name, rels })));
if (this.eventHandlers.onConnect) {
return this.eventHandlers.onConnect(this);
}
}

/**
* @return Promise<null>
*/
disconnect() {
return resolveAllKeys(
mapKeys(this.adapters, adapter => adapter.disconnect())
// Chain an empty function so that the result of this promise
// isn't unintentionally leaked to the caller
).then(() => {});
async disconnect() {
await resolveAllKeys(mapKeys(this.adapters, adapter => adapter.disconnect()));
}

getAdminMeta({ schemaName }) {
Expand Down
8 changes: 5 additions & 3 deletions packages/keystone/lib/List/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,15 +826,17 @@ module.exports = class List {
// Return these as functions so they're lazily evaluated depending
// on what the user requested
// Evaluation takes place in ../Keystone/index.js
getCount: () => {
getCount: async () => {
const access = this.checkListAccess(context, undefined, 'read', { gqlName });

return this._itemsQuery(mergeWhereClause(args, access), {
const { count } = await this._itemsQuery(mergeWhereClause(args, access), {
meta: true,
context,
info,
from,
}).then(({ count }) => count);
});

return count;
},
};
}
Expand Down
27 changes: 14 additions & 13 deletions packages/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const mapKeyNames = (obj, func) =>
{}
);

export const resolveAllKeys = obj => {
export const resolveAllKeys = async obj => {
const returnValue = {};
const errors = {};

Expand All @@ -38,18 +38,19 @@ export const resolveAllKeys = obj => {
})
);

return Promise.all(allPromises).then(results => {
// If there are any errors, we want to surface them in the same shape as the
// input object
if (Object.keys(errors).length) {
const firstError = results.find(({ isRejected }) => isRejected).reason;
// Use the first error as the message so it's at least meaningful
const error = new Error(firstError.message || firstError.toString());
error.errors = errors;
throw error;
}
return returnValue;
});
const results = await Promise.all(allPromises);

// If there are any errors, we want to surface them in the same shape as the
// input object
if (Object.keys(errors).length) {
const firstError = results.find(({ isRejected }) => isRejected).reason;
// Use the first error as the message so it's at least meaningful
const error = new Error(firstError.message || firstError.toString());
error.errors = errors;
throw error;
}

return returnValue;
};

export const unique = arr => [...new Set(arr)];
Expand Down

0 comments on commit e0e3e30

Please sign in to comment.