axios.get('https://jsonplaceholder.typicode.com/todos/1')
.then(async function (response) {
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for await (const item of items) {
const res = await axios.get(`https://jsonplaceholder.typicode.com/users/${item}`)
fs.writeFileSync('test.json', JSON.stringify(res.data.phone, null, 2));
}
console.log(response.data);
})
.catch(function (error) {
console.log(error);
})
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for (const item of items) {
const res = await axios.get(`https://jsonplaceholder.typicode.com/users/${item}`) // Ждет заверения запроса
await fs.promises.writeFile('test.json', JSON.stringify(res.data.phone, null, 2)); // Ждет завершения записи
await ... // Ждет завершения любого промиса
}
const promise = axios.get('https://jsonplaceholder.typicode.com/todos/1')
promise
.then(response => ({ response, items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }))
.then(nextThen)
.catch(console.log)
const nextThen = ({ response, items }) => {
if (!items.length) {
console.log(response)
return
}
const [nextValue, ...rest] = items
promise
.then(item => axios.get(`https://jsonplaceholder.typicode.com/users/${item}`))
.then(res => {
fs.writeFileSync('test.json', JSON.stringify(res.data.phone, null, 2))
return ( { response, items: rest })
})
.then(nextThen)
return nextValue
}