Last active
April 11, 2017 15:04
-
-
Save kharandziuk/048fec4818b4499af2c27cff7dd4c341 to your computer and use it in GitHub Desktop.
async-vs-promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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