0

So essentially I am using the node-url-shortener library to shorten a link, however, after using that function I want to pass the new shortened link (shortenedUrl) outside of the function and then log it in the console.

If I throw the console.log inside the function it logs perfectly fine but obviously that is not what I'm trying to achieve here...

This was my attempt at doing so:

const shortUrl = require('node-url-shortener');

var shortenedUrl;

shortUrl.short('www.google.com', function(err, shortUrl){
  shortenedUrl = shortUrl;
  // do somemthing here to pass the shortenedUrl outside the function
});

console.log(shortenedUrl);

Any help is appreciated and thanks in advance!

James
  • 197
  • 1
  • 1
  • 10
  • no you have to put your logic inside, this is called *"callback"*. outside code runs sequentially, when .short finishes, it calls your _callback_ – Eric Wong Nov 05 '19 at 09:43
  • Use promise or call back function concept over here. Above code execution in sequence way, so that you will get "undefined" as a output in console. – Kunalan Krish Nov 05 '19 at 09:50
  • @EricWong could you please explain how & where to implement a callback in my script above? – James Nov 05 '19 at 09:54

0 Answers0