Skip to content

Instantly share code, notes, and snippets.

@velizarn
Last active January 3, 2018 08:17
Show Gist options
  • Save velizarn/32dddce34ec4b6dda54423224b8fc09a to your computer and use it in GitHub Desktop.
Save velizarn/32dddce34ec4b6dda54423224b8fc09a to your computer and use it in GitHub Desktop.
Run array of promises and stop on first failure
'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]
asyncArray.reduce((cur, next) => {
return cur.then(() => next())
}, Promise.resolve())
.then(() => {
console.log('all executed')
console.log('Chain ended')
console.log(JSON.stringify(state, null, 4))
})
.catch((err) => {
console.error(err + '')
console.log('Chain ended')
console.log(JSON.stringify(state, null, 4))
})
/*
Result:
Chain ended
{
"time": {
"epoch": 1514967283,
"slang_date": "today",
"slang_time": "now",
"iso8601": "2018-01-03T08:14:43Z",
"rfc2822": "Wed, 03 Jan 2018 08:14:43 GMT",
"rfc3339": "2018-01-03T08:14:43.00Z"
},
"user": {
"error": "Error: Protocol \"http:\" not supported. Expected \"https:\""
},
"photo": null,
"book": null
}
*/
'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