Skip to content

Instantly share code, notes, and snippets.

@kharandziuk
Last active April 11, 2017 15:04
Show Gist options
  • Select an option

  • Save kharandziuk/048fec4818b4499af2c27cff7dd4c341 to your computer and use it in GitHub Desktop.

Select an option

Save kharandziuk/048fec4818b4499af2c27cff7dd4c341 to your computer and use it in GitHub Desktop.
async-vs-promise
const Promise = require('bluebird')
function gogogo(queryList) {
return Promise
.resolve(queryList)
.map(getType)
.map(function(type) { // can be incapsulated in a function but original sample doesn't do that
if (type === 1) {
return getJson(query)
}
else if (type === 2) {
return getRawData(query)
.rawToJson(rawData)
}
else {
return {}
}
})
.reduce(Object.assign, {})
}
function doSomething() {
gogogo(queryList)
.then(templateGenerator)
.tap(console.log)
}
doSomething()
//original version
const Promise = require('bluebird')
async function gogogo(queryList) {
let resultJson = {}
for (const query of queryList) {
let json = {}
const type = await getType(query)
if (type === 1) {
json = await getJson(query)
}
else if (type === 2) {
const rawData = await getRawData(query)
json = await rawToJson(rawData)
}
else {
break
}
Object.assign(resultJson, json)
}
return resultJson
}
async function doSomething() {
const resultJson = await gogogo(queryList)
const html = templateGenerator(resultJson)
console.log(hmtl)
}
doSomething()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment