This question is the same as
How can I fetch an array of URLs with Promise.all? except that I want an answer that
does not use Promise.all
.
1
The array of URLs:
urls = ['https://jsonplaceholder.typicode.com/todos/2',
'https://jsonplaceholder.typicode.com/todos/3']
The JSONs of the URLs:
{"userId":1,"id":2,"title":"quis ut nam facilis et officia qui",
"completed":false}
{"userId":1,"id":3,"title":"fugiat veniam minus","completed":false}
The goal is to get an array of objects, where each object contains the title
value from the corresponding URL.
To make it a little more interesting, I will assume that there is already an array of names that I want the array of titles to be merged with:
namesonly = ['two', 'three']
The desired output is an array of objects:
[{"name":"two","loremipsum":"quis ut nam facilis et officia qui"},
{"name":"three","loremipsum":"fugiat veniam minus"}]
where I have changed the attribute name title
to loremipsum
.
1 The specific reason I want a solution not using Promise.all
is that I want JavaScript code that works in Postman scripts.
The latter don't (yet) support promises natively, at the time of writing.