Shikamaru z konoszki
Shikamaru z konoszki

Reputation: 11

Figlet executing after code, that should be executed earlier

I have some basic code for CLI:

#!/usr/bin/env node

import chalk from "chalk"
import inquirer from "inquirer"
import gradient from "gradient-string"
import figlet from "figlet"
import { createSpinner } from "nanospinner"

const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms))

async function intro() {
  await figlet("Inkwell CLI", (err, data) => {
    console.log(gradient.mind.multiline(data))
  })
}

await intro().then(() => {
  console.log("XD")
})

and when I'm executing this i get that: (https://i.sstatic.net/6DPsL.png) How to fix this?

I tried to console.log figlet earlier than doing rest of my code.

Upvotes: 0

Views: 391

Answers (1)

Jeffin Manuel
Jeffin Manuel

Reputation: 542

This is because figlet is executed async internally. So you have to wait for sometime for figlet to be completed. sleep() should help achieve this. So you could invoke sleep() as the last line of your intro(). You might have to adjust the waiting time by trial and error.

async function intro() {
  await figlet("Inkwell CLI", (err, data) => {
    console.log(gradient.mind.multiline(data))
  })
  sleep()
}

You could also leverage AbortController to signal when exactly to break from sleep.

Upvotes: 0

Related Questions