mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
refactor Unnecessary 'await'
This commit is contained in:
3
packages/common/cache/cache.module.ts
vendored
3
packages/common/cache/cache.module.ts
vendored
@@ -55,8 +55,7 @@ export class CacheModule {
|
||||
}
|
||||
return {
|
||||
provide: CACHE_MODULE_OPTIONS,
|
||||
useFactory: async (optionsFactory: CacheOptionsFactory) =>
|
||||
await optionsFactory.createCacheOptions(),
|
||||
useFactory: async (optionsFactory: CacheOptionsFactory) => optionsFactory.createCacheOptions(),
|
||||
inject: [options.useExisting || options.useClass],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -52,8 +52,7 @@ export class MulterModule {
|
||||
}
|
||||
return {
|
||||
provide: MULTER_MODULE_OPTIONS,
|
||||
useFactory: async (optionsFactory: MulterOptionsFactory) =>
|
||||
await optionsFactory.createMulterOptions(),
|
||||
useFactory: async (optionsFactory: MulterOptionsFactory) => optionsFactory.createMulterOptions(),
|
||||
inject: [options.useExisting || options.useClass],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ export class ExternalContextCreator {
|
||||
callback,
|
||||
handler(initialArgs, ...args),
|
||||
);
|
||||
return await this.transformToResult(result);
|
||||
return this.transformToResult(result);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ export class ExternalContextCreator {
|
||||
{ metatype, type, data },
|
||||
transforms: Transform<any>[],
|
||||
): Promise<any> {
|
||||
return await this.pipesConsumer.apply(
|
||||
return this.pipesConsumer.apply(
|
||||
value,
|
||||
{ metatype, type, data },
|
||||
transforms,
|
||||
@@ -214,7 +214,7 @@ export class ExternalContextCreator {
|
||||
|
||||
public async transformToResult(resultOrDeffered) {
|
||||
if (resultOrDeffered && isFunction(resultOrDeffered.subscribe)) {
|
||||
return await resultOrDeffered.toPromise();
|
||||
return resultOrDeffered.toPromise();
|
||||
}
|
||||
return resultOrDeffered;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ export class Injector {
|
||||
module: Module,
|
||||
) {
|
||||
if (wrapper.isPending) {
|
||||
return await wrapper.done$;
|
||||
return wrapper.done$;
|
||||
}
|
||||
const done = this.applyDoneHook(wrapper);
|
||||
const { metatype, name, inject } = wrapper;
|
||||
@@ -220,7 +220,7 @@ export class Injector {
|
||||
throw new UndefinedDependencyException(wrapper.name, dependencyContext);
|
||||
}
|
||||
const token = this.resolveParamToken(wrapper, param);
|
||||
return await this.resolveComponentInstance<T>(
|
||||
return this.resolveComponentInstance<T>(
|
||||
module,
|
||||
isFunction(token) ? (token as Type<any>).name : token,
|
||||
dependencyContext,
|
||||
@@ -275,7 +275,7 @@ export class Injector {
|
||||
module,
|
||||
wrapper,
|
||||
);
|
||||
return components.has(name) ? components.get(name) : await scanInExports();
|
||||
return components.has(name) ? components.get(name) : scanInExports();
|
||||
}
|
||||
|
||||
public async lookupComponentInExports<T = any>(
|
||||
|
||||
@@ -52,8 +52,7 @@ export class InstanceLoader {
|
||||
private async createInstancesOfComponents(module: Module) {
|
||||
await Promise.all(
|
||||
[...module.components.values()].map(
|
||||
async wrapper =>
|
||||
await this.injector.loadInstanceOfComponent(wrapper, module),
|
||||
async wrapper => this.injector.loadInstanceOfComponent(wrapper, module),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -67,8 +66,7 @@ export class InstanceLoader {
|
||||
private async createInstancesOfRoutes(module: Module) {
|
||||
await Promise.all(
|
||||
[...module.routes.values()].map(
|
||||
async wrapper =>
|
||||
await this.injector.loadInstanceOfRoute(wrapper, module),
|
||||
async wrapper => this.injector.loadInstanceOfRoute(wrapper, module),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -85,8 +83,7 @@ export class InstanceLoader {
|
||||
private async createInstancesOfInjectables(module: Module) {
|
||||
await Promise.all(
|
||||
[...module.injectables.values()].map(
|
||||
async wrapper =>
|
||||
await this.injector.loadInstanceOfInjectable(wrapper, module),
|
||||
async wrapper => this.injector.loadInstanceOfInjectable(wrapper, module),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ export class InterceptorsConsumer {
|
||||
next: () => Promise<any>,
|
||||
): Promise<any> {
|
||||
if (isEmpty(interceptors)) {
|
||||
return await await next();
|
||||
return next();
|
||||
}
|
||||
const context = this.createContext(args, instance, callback);
|
||||
const start$ = defer(() => this.transformDeffered(next));
|
||||
@@ -27,11 +27,10 @@ export class InterceptorsConsumer {
|
||||
};
|
||||
*/
|
||||
const result$ = await interceptors.reduce(
|
||||
async (stream$, interceptor) =>
|
||||
await interceptor.intercept(context, await stream$),
|
||||
async (stream$, interceptor) => interceptor.intercept(context, await stream$),
|
||||
Promise.resolve(start$),
|
||||
);
|
||||
return await result$.toPromise();
|
||||
return result$.toPromise();
|
||||
}
|
||||
|
||||
public createContext(
|
||||
|
||||
@@ -11,8 +11,7 @@ export class MiddlewareResolver {
|
||||
const middleware = this.middlewareContainer.getMiddleware(moduleName);
|
||||
await Promise.all(
|
||||
[...middleware.values()].map(
|
||||
async wrapper =>
|
||||
await this.resolveMiddlewareInstance(wrapper, middleware, module),
|
||||
async wrapper => this.resolveMiddlewareInstance(wrapper, middleware, module),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ export const filterMiddleware = middleware => {
|
||||
return []
|
||||
.concat(middleware)
|
||||
.filter(isFunction)
|
||||
.map(ware => mapToClass(ware));
|
||||
.map(mapToClass);
|
||||
};
|
||||
|
||||
export const mapToClass = middleware => {
|
||||
|
||||
@@ -91,7 +91,7 @@ export class NestApplicationContext extends ModuleRef
|
||||
.map(([key, { instance }]) => instance)
|
||||
.filter(instance => !isNil(instance))
|
||||
.filter(this.hasOnModuleInitHook)
|
||||
.map(async instance => await (instance as OnModuleInit).onModuleInit()),
|
||||
.map(async instance => (instance as OnModuleInit).onModuleInit()),
|
||||
);
|
||||
if (moduleClassInstance && this.hasOnModuleInitHook(moduleClassInstance)) {
|
||||
await (moduleClassInstance as OnModuleInit).onModuleInit();
|
||||
@@ -121,10 +121,7 @@ export class NestApplicationContext extends ModuleRef
|
||||
.map(([key, { instance }]) => instance)
|
||||
.filter(instance => !isNil(instance))
|
||||
.filter(this.hasOnModuleDestroyHook)
|
||||
.map(
|
||||
async instance =>
|
||||
await (instance as OnModuleDestroy).onModuleDestroy(),
|
||||
),
|
||||
.map(async instance => (instance as OnModuleDestroy).onModuleDestroy()),
|
||||
);
|
||||
if (
|
||||
moduleClassInstance &&
|
||||
@@ -155,9 +152,8 @@ export class NestApplicationContext extends ModuleRef
|
||||
.map(([key, { instance }]) => instance)
|
||||
.filter(instance => !isNil(instance))
|
||||
.filter(this.hasOnAppBotstrapHook)
|
||||
.map(
|
||||
async instance =>
|
||||
await (instance as OnApplicationBootstrap).onApplicationBootstrap(),
|
||||
.map(async instance =>
|
||||
(instance as OnApplicationBootstrap).onApplicationBootstrap(),
|
||||
),
|
||||
);
|
||||
if (moduleClassInstance && this.hasOnAppBotstrapHook(moduleClassInstance)) {
|
||||
|
||||
@@ -117,7 +117,7 @@ export class NestFactoryStatic {
|
||||
const context = this.createNestInstance<NestApplicationContext>(
|
||||
new NestApplicationContext(container, [], root),
|
||||
);
|
||||
return await context.init();
|
||||
return context.init();
|
||||
}
|
||||
|
||||
private createNestInstance<T>(instance: T): T {
|
||||
|
||||
@@ -10,7 +10,7 @@ export class PipesConsumer {
|
||||
transforms: Transform<any>[],
|
||||
) {
|
||||
const token = this.paramsTokenFactory.exchangeEnumForString(type);
|
||||
return await this.applyPipes(
|
||||
return this.applyPipes(
|
||||
value,
|
||||
{ metatype, type: token, data },
|
||||
transforms,
|
||||
@@ -22,7 +22,7 @@ export class PipesConsumer {
|
||||
{ metatype, type, data }: { metatype; type?; data? },
|
||||
transforms: Transform<any>[],
|
||||
) {
|
||||
return await transforms.reduce(async (defferedValue, fn) => {
|
||||
return transforms.reduce(async (defferedValue, fn) => {
|
||||
const val = await defferedValue;
|
||||
const result = fn(val, { metatype, type, data });
|
||||
return result;
|
||||
|
||||
@@ -186,7 +186,7 @@ export class RouterExecutionContext {
|
||||
type === RouteParamtypes.PARAM ||
|
||||
isString(type)
|
||||
) {
|
||||
return await this.pipesConsumer.apply(
|
||||
return this.pipesConsumer.apply(
|
||||
value,
|
||||
{ metatype, type, data },
|
||||
transforms,
|
||||
|
||||
@@ -21,7 +21,7 @@ export class RouterResponseController {
|
||||
|
||||
public async transformToResult(resultOrDeffered) {
|
||||
if (resultOrDeffered && isFunction(resultOrDeffered.subscribe)) {
|
||||
return await resultOrDeffered.toPromise();
|
||||
return resultOrDeffered.toPromise();
|
||||
}
|
||||
return resultOrDeffered;
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ export class DependenciesScanner {
|
||||
|
||||
public async storeModule(module: any, scope: Type<any>[]) {
|
||||
if (module && module.forwardRef) {
|
||||
return await this.container.addModule(module.forwardRef(), scope);
|
||||
return this.container.addModule(module.forwardRef(), scope);
|
||||
}
|
||||
await this.container.addModule(module, scope);
|
||||
}
|
||||
@@ -256,7 +256,7 @@ export class DependenciesScanner {
|
||||
throw new CircularDependencyException(context);
|
||||
}
|
||||
if (related && related.forwardRef) {
|
||||
return await this.container.addRelatedModule(related.forwardRef(), token);
|
||||
return this.container.addRelatedModule(related.forwardRef(), token);
|
||||
}
|
||||
await this.container.addRelatedModule(related, token);
|
||||
}
|
||||
|
||||
@@ -74,16 +74,16 @@ describe('InterceptorsConsumer', () => {
|
||||
const val = 3;
|
||||
const next = async () => val;
|
||||
expect(
|
||||
await await consumer.transformDeffered(next).toPromise(),
|
||||
await consumer.transformDeffered(next).toPromise(),
|
||||
).to.be.eql(val);
|
||||
});
|
||||
});
|
||||
describe('when next() result is Promise', () => {
|
||||
it('should return Observable', async () => {
|
||||
const val = 3;
|
||||
const next = () => Promise.resolve(val);
|
||||
const next = async () => val;
|
||||
expect(
|
||||
await await consumer.transformDeffered(next).toPromise(),
|
||||
await consumer.transformDeffered(next).toPromise(),
|
||||
).to.be.eql(val);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -30,7 +30,7 @@ export abstract class ClientProxy {
|
||||
if (isNil(pattern) || isNil(data)) {
|
||||
return _throw(new InvalidMessageException());
|
||||
}
|
||||
return defer(async () => await this.connect()).pipe(
|
||||
return defer(async () => this.connect()).pipe(
|
||||
mergeMap(
|
||||
() =>
|
||||
new Observable((observer: Observer<TResult>) => {
|
||||
|
||||
@@ -56,7 +56,7 @@ export class RpcContextCreator {
|
||||
return this.rpcProxy.create(async (...args) => {
|
||||
fnCanActivate && (await fnCanActivate(args));
|
||||
|
||||
return await this.interceptorsConsumer.intercept(
|
||||
return this.interceptorsConsumer.intercept(
|
||||
interceptors,
|
||||
args,
|
||||
instance,
|
||||
|
||||
@@ -120,7 +120,7 @@ export class NestMicroservice extends NestApplicationContext
|
||||
}
|
||||
|
||||
public async listenAsync(): Promise<any> {
|
||||
return await new Promise(resolve => this.listen(resolve));
|
||||
return new Promise(resolve => this.listen(resolve));
|
||||
}
|
||||
|
||||
public async close(): Promise<any> {
|
||||
|
||||
@@ -58,8 +58,7 @@ export class ServerMqtt extends Server implements CustomTransportStrategy {
|
||||
}
|
||||
|
||||
public getMessageHandler(pub: MqttClient): any {
|
||||
return async (channel, buffer) =>
|
||||
await this.handleMessage(channel, buffer, pub);
|
||||
return async (channel, buffer) => this.handleMessage(channel, buffer, pub);
|
||||
}
|
||||
|
||||
public async handleMessage(
|
||||
|
||||
@@ -64,8 +64,7 @@ export class ServerNats extends Server implements CustomTransportStrategy {
|
||||
}
|
||||
|
||||
public getMessageHandler(channel: string, client: Client) {
|
||||
return async (buffer, replyTo: string) =>
|
||||
await this.handleMessage(channel, buffer, client, replyTo);
|
||||
return async (buffer, replyTo: string) => this.handleMessage(channel, buffer, client, replyTo);
|
||||
}
|
||||
|
||||
public async handleMessage(
|
||||
|
||||
@@ -72,8 +72,7 @@ export class ServerRedis extends Server implements CustomTransportStrategy {
|
||||
}
|
||||
|
||||
public getMessageHandler(pub: RedisClient) {
|
||||
return async (channel, buffer) =>
|
||||
await this.handleMessage(channel, buffer, pub);
|
||||
return async (channel, buffer) => this.handleMessage(channel, buffer, pub);
|
||||
}
|
||||
|
||||
public async handleMessage(channel, buffer: string | any, pub: RedisClient) {
|
||||
|
||||
@@ -42,7 +42,7 @@ export class ServerTCP extends Server implements CustomTransportStrategy {
|
||||
const readSocket = this.getSocketInstance(socket);
|
||||
readSocket.on(
|
||||
MESSAGE_EVENT,
|
||||
async msg => await this.handleMessage(readSocket, msg),
|
||||
async msg => this.handleMessage(readSocket, msg),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ export class WsContextCreator {
|
||||
return this.wsProxy.create(async (...args) => {
|
||||
fnCanActivate && (await fnCanActivate(args));
|
||||
|
||||
return await this.interceptorsConsumer.intercept(
|
||||
return this.interceptorsConsumer.intercept(
|
||||
interceptors,
|
||||
args,
|
||||
instance,
|
||||
|
||||
@@ -73,7 +73,7 @@ export class SocketModule {
|
||||
const servers = this.socketsContainer.getAllServers();
|
||||
await Promise.all(
|
||||
iterate(servers.values()).map(
|
||||
async ({ server }) => server && (await adapter.close(server)),
|
||||
async ({ server }) => server && adapter.close(server),
|
||||
),
|
||||
);
|
||||
this.socketsContainer.clear();
|
||||
|
||||
Reference in New Issue
Block a user