简体   繁体   中英

Array of shorted URLs using foreach loop through and how to resolve each url with nodejs TALL

In my problem issue is very simple but I do not get it, HOW to do it!

I have 2 shortened URL in a array; [ 'URL1', 'URL2' ] and I am trying to use foreach with TALL to open each shortened url and check the REAL url.

I cant get it done, at least https://www.npmjs.com/package/tall

if (data) {

    let urls = getUrls(www);
    let array = Array.from(urls);


    var urlit = array.map((aurl) => aurl);


    for(var i = 0; i < urlit.length; i++){
        var urlData = urlit[i];
        let newUrls = tall(urlit[i]);

        context.log(i+1, urlData);
        context.log(newUrls);
     };

All-in-all; I have an array of urls => how it open each url and if its correct one and add it into variable? (as Tall use promises)

Hope I am enough clear with the issue and thanks in advance for your help!

You can try something like this (StackOverflow does not allow to post shortened URLs even in code fragments, so I've masked them by concatenating):

'use strict';

const { tall } = require('tall');

const urls = [
  'https://' + 't.co/og6viaRxnr',
  'https://' + 't.co/pIq3NBQIwD',
];

(async function asyncFunc() {
  try {
    for (const url of urls) {
      const unshortenedUrl = await tall(url);
      console.log(unshortenedUrl);
    }
  } catch (err) {
    console.error(err);
  }
})();

Output:

https://github.blog/changelog/2019-02-21-organization-wide-community-health-files/
https://github.blog/changelog/2019-02-21-re-request-review-on-a-pull-request/

====================== for the fragment in the comment:

'use strict';

const { tall } = require('tall');

(async function asyncFunc() {
  try {
    if (data) {
      let urls = getUrls(www);
      let array = Array.from(urls);
      var urlit = array.map((aurl) => aurl);


      for(var i = 0; i < urlit.length; i++){
        var urlData = urlit[i];
        let newUrls = await tall(urlit[i]);

        context.log(i+1, urlData);
        context.log(newUrls);
      };
    }
  } catch (err) {
    console.error(err);
  }
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM