Skip to content

Instantly share code, notes, and snippets.

@devansvd
Created March 19, 2018 10:53
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 devansvd/075a1e4522579fa165507122a1a6e5e6 to your computer and use it in GitHub Desktop.
Save devansvd/075a1e4522579fa165507122a1a6e5e6 to your computer and use it in GitHub Desktop.
callback to promise example
const getCourses = () => {
return new Promise( (resolve, reject) => {
S3.getObject({
Bucket: 'your-bucket-here',
Key: 'your-file.json'
}, function (err, data){
if (err !== null){
reject(err)
}
resolve(JSON.parse(data.Body.toString('utf-8')))
})
})
}
getCourses().
then( data => {
let message
let courses = data.data.filter(course => {
return (course.subject_desc.toLowerCase().includes(CourseType))
})
if (!courses.length > 0){
message = "Sorry, we couldn't find any courses in that discipline or at that level. Let me know if want me to look again for something else."
} else {
message = `We found ${courses.length} courses: \n
${courses.map(course => `${course.subject} ${course.course_number}-${course.section}: ${course.title}`).join('\n')}
`
}
let response = {
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": message
}
}
}
callback(null, response)
}).catch(err => callback(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment