cronvel / terminal-kit

Terminal utilities for node.js
MIT License
3.08k stars 198 forks source link

drawImage( ... ) draws Image not until end of program. #201

Closed Sayum24 closed 2 years ago

Sayum24 commented 2 years ago

When I want to draw an Image I want to stop the program, until the image is finished.

const term = require('terminal-kit').terminal;
const getPixels = require("get-pixels")

drawImage();

console.log('Test123')

function drawImage(){
  term.drawImage("logo.png", {
  shrink: {
    width: 100,
    height: 100
  }
})

  return;
}

This code prints first Test123 and then starts to print the image. How can I change that?

Sayum24 commented 2 years ago

Found this way to print in the program: Make the function which calls the function to draw the Image into a async function.

const term = require('terminal-kit').terminal;
const getPixels = require("get-pixels")

test();

async function drawImage() {
  await term.drawImage("logo.png", {
    shrink: {
      width: 50,
      height: 50
    }
  }, )

  return;
}

async function test(){
  console.log('before')
  await drawImage();
  console.log('after')
}

Is this the only way to make this happen?

cronvel commented 2 years ago

@Sayum24 From the doc, you'll see that .drawImage() is not sync. You have to pass a callback (old API) OR await for the returning promise.

In Javascript, async/await/promise are very common concept you should be familiar with.