TypeScript
async IIFE vs. Promise initialization

by Benny Neugebauer

ConfigSaver.load(cli.config)
.then(async (config: SerializedTradingConfig[]) => {
  config.forEach(async tradingConfig => {
    const handler = new TrailingStopHandler(tradingConfig);
    const stopConfig = await handler.init();
    const intervalID = setInterval(async () => {
      const status = await handler.watch();
      if (status === StopPriceState.REACHED) {
        clearInterval(intervalID);
      } else if (status === StopPriceState.INCREASED) {
        const updates = stopConfig.serialize();
        tradingConfig.price = updates.pricePerCoin;
        tradingConfig.reset = updates.trailingReset;
        tradingConfig.stop = updates.trailingStop;
        ConfigSaver.save(cli.config, config);
      }
    }, ms(tradingConfig.updateInterval));
  });
});

(async () => {
  const config = await ConfigSaver.load(cli.config);
  config.forEach(async tradingConfig => {
    const handler = new TrailingStopHandler(tradingConfig);
    const stopConfig = await handler.init();
    const intervalID = setInterval(async () => {
      const status = await handler.watch();
      if (status === StopPriceState.REACHED) {
        clearInterval(intervalID);
      } else if (status === StopPriceState.INCREASED) {
        const updates = stopConfig.serialize();
        tradingConfig.price = updates.pricePerCoin;
        tradingConfig.reset = updates.trailingReset;
        tradingConfig.stop = updates.trailingStop;
        ConfigSaver.save(cli.config, config);
      }
    }, ms(tradingConfig.updateInterval));
  });
})();