lmammino / tall

Promise-based, No-dependency URL unshortner (expander) module for Node.js
https://lmammino.github.io/tall/
MIT License
72 stars 7 forks source link

Array of urls not able to resolved #6

Closed nintendoMan closed 4 years ago

nintendoMan commented 5 years ago

Hi,

As investigating while trying to get this work with array of urls => does not work? If there is a away to do it, write it down?

Simple as but not able to get it work.

fiblan commented 5 years ago

A solution could be use Promise.all + map. I'm sure that you could write it cleaner...

var tall = require('tall').default

var urlArray = [
  'http://loige.link/b',
  'http://loige.link/gh',
  'http://loige.link/codemotion-rome-2017'
 ];

async function tallMultiple(arrayUrl) {
  try {
    let unshortenedUrls = await Promise.all(urlArray.map(async (item) =>{return await tall(item)}))
    console.log(unshortenedUrls);
  } catch( err) {
    console.error('AAAW 👻', err)
  }
}

tallMultiple(urlArray)
lmammino commented 5 years ago

Tall already returns a promise, so you can rewrite the above proposal by @fiblan as follows:

const tall = require('tall').default

const tallMany = (urls) => Promise.all( urls.map( url => tall(url) ) )

async function main() {
  const urls = [
    'http://loige.link/b',
    'http://loige.link/gh',
    'http://loige.link/codemotion-rome-2017'
  ]

  const expandendUrls = await tallMany(urls)
  console.log(expandendUrls) // ['https://www.nodejsdesignpatterns.com/', 'https://github.com/lmammino', 'https://slides.com/lucianomammino/universal-js-web-applications-with-react-codemotion-rome-2017']
}

main()

▶️ Run this code on runkit.com

Is this such a common use case to make sense to introduce something like tallMany here in the library itself?

In any case, let me know if this works for you :)

lmammino commented 4 years ago

Closing this one. Le t me know if you still have issues