Skip to content

Instantly share code, notes, and snippets.

@velizarn
Last active January 3, 2018 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velizarn/684d8d72169b0f9042e1adce64c22000 to your computer and use it in GitHub Desktop.
Save velizarn/684d8d72169b0f9042e1adce64c22000 to your computer and use it in GitHub Desktop.
Run array of promises despite of the result
'use strict'
global.state = {
time: null,
user: null,
photo: null,
book: null
}
const { getTime, getUser, getPhoto, getBook } = require('./promise_modules')
let asyncArray = [getTime, getUser, getPhoto, getBook]
let chain = Promise.resolve()
for (const func of asyncArray) {
chain = chain.then(() => func()).catch((err) => { /* console.error(err + '') */ })
}
chain.then(() => {
console.log('Chain ended')
console.log(JSON.stringify(state, null, 4))
})
/*
Result:
Chain ended
{
"time": {
"epoch": 1514967155,
"slang_date": "today",
"slang_time": "a second ago",
"iso8601": "2018-01-03T08:12:35Z",
"rfc2822": "Wed, 03 Jan 2018 08:12:35 GMT",
"rfc3339": "2018-01-03T08:12:35.00Z"
},
"user": {
"error": "Error: Protocol \"http:\" not supported. Expected \"https:\""
},
"photo": {
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/92c952"
},
"book": {
"status": 404,
"error": {
"Message": "No HTTP resource was found that matches the request URI 'https://fakerestapi.azurewebsites.net/api/Booksa/a'.",
"MessageDetail": "No type was found that matches the controller named 'Booksa'."
}
}
}
*/
'use strict'
const https = require('https'), http = require('http')
let getTime = () => {
return new Promise((resolve, reject) => {
try {
let url = 'https://now.httpbin.org/when/' + (new Date()).getTime()
https.get(url, (res) => {
let body = '';
res.on('data', (chunk) => { body += chunk })
res.on('end', () => {
let response = JSON.parse(body)
state.time = response.timestamp
resolve()
})
}).on('error', (e) => {
return reject(e + '')
})
} catch (e) {
return reject(e + '')
}
})
}
let getUser = () => {
return new Promise((resolve, reject) => {
try {
let url = 'http://jsonplaceholder.typicode.com/users/8'
https.get(url, (res) => {
let body = '';
res.on('data', (chunk) => { body += chunk })
res.on('end', () => {
let response = JSON.parse(body)
state.user = response
resolve()
})
}).on('error', (e) => {
return reject(e + '')
})
} catch (e) {
state.user = { error: e + '' }
return reject(e + '')
}
})
}
let getPhoto = () => {
return new Promise((resolve, reject) => {
try {
let url = 'https://jsonplaceholder.typicode.com/photos/1'
https.get(url, (res) => {
let body = '';
res.on('data', (chunk) => { body += chunk })
res.on('end', () => {
let response = JSON.parse(body)
state.photo = response
resolve()
})
}).on('error', (e) => {
return reject(e + '')
})
} catch (e) {
return reject(e + '')
}
})
}
let getBook = () => {
return new Promise((resolve, reject) => {
try {
let url = 'https://fakerestapi.azurewebsites.net/api/Booksa/a'
https.get(url, (res) => {
let body = '';
res.on('data', (chunk) => { body += chunk })
res.on('end', () => {
let response = JSON.parse(body)
if (res.statusCode === 200) { state.book = response }
else if (res.statusCode >= 400 && res.statusCode < 500) { state.book = { status: res.statusCode, error: response } }
resolve()
})
}).on('error', (e) => {
state.book = { error: e + '' }
return reject(e + '')
})
} catch (e) {
state.book = { error: e + '' }
return reject(e + '')
}
})
}
module.exports = {
getTime: getTime,
getUser: getUser,
getPhoto: getPhoto,
getBook: getBook
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment