如何将axios响应数据添加到数组中

时间:2019-01-04 00:44:13

标签: javascript arrays asynchronous axios

我正在尝试将URL数组异步发送到缩短URL的API并将响应写入新数组,我的问题是:

在解决所有诺言之后如何执行某些事情?

我的代码

#myUL,*{margin:0}*{box-sizing:border-box}#myInput{width:100%;padding:12px 23px 11px 13px}#myUL{list-style-type:none;padding:0}#myUL li a{padding:10px 10px 10px 15px;text-decoration:none;color:#000;display:block;text-align:left}#myUL li a:hover:not(.header){background-color:#ccc}.accor{background-color:#eee;color:#444;padding:18px;width:100%;border:none;outline:0;font-size:20px}.accor:hover,.active{background-color:#ccc}.accor:after{content:'open';color:#777;font-weight:700;float:right;margin-right:5px}.active:after{content:"close"}.panel{max-height:0;overflow:hidden;transition:max-height .1s ease-out;text-align:left;font-size:12px}.title{font-size:20px}.otras{font-style:italic}

我希望<input type="text" id="myInput" data-filter="#myUL" placeholder="Search..." autocomplete=off> <ul id="myUL"> <li> <a class="accor">1House</a> <div class="panel"> <div class="otras"> <p class=title>2Search by this too</p> <p>3This is a sentence</p> </div> <div class="otras"> <p class=title>4Also this</p> <p>5Another sentence to search </p> </div> <div class="otras"> <p class=title>6And this too</p> <p>7Example of sentence</p> </div> </div> </li> <li> <a class="accor">8Search here</a> <div class="panel"> <div class="otras"> <p class=title> 9A different set of title </p> <p>10A different set of </p> </div> <div class="otras"> <p class=title>11Also this</p> <p>12Another sentence to search </p> </div> <div class="otras"> <p class=title>13And this too</p> <p>14Example of sentence</p> </div> </div> </li> </ul>中有一系列缩短的URL

1 个答案:

答案 0 :(得分:1)

我猜测axios实际上是您的异步​​请求,在这种情况下,这应该是获取缩短的URL列表所需要的:

const linkList = ['https://www.link1.com', 'https://www.link2.com'];
const adsList = await Promise.all(
  linkList.map(url => 
    axios(apiUrl + url).then(({data: shortenedUrl}) => shortenedUrl)
  )
);