使用foreach循环遍历的简短URL数组以及如何使用Node.js Tall解析每个URL

时间:2019-02-22 09:24:57

标签: javascript node.js promise

在我的问题中,问题很简单,但是我不明白,怎么做!

我在数组中有2个缩短的URL; ['URL1',   “ URL2” ] 并且我正尝试将Teach与foreach一起使用以打开每个缩短的网址并检查REAL网址。

至少我做不到 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);
     };

全能;我有一个网址数组=>它如何打开每个网址,如果它是正确的网址并将其添加到变量中? (正如“高使用承诺”一样)

希望我已经很清楚这个问题了,在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的操作(即使在代码段中,StackOverflow也不允许发布缩短的URL,因此我通过串联来屏蔽它们):

'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);
  }
})();

输出:

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/

========================评论中的片段:

'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);
  }
})();