Created
May 24, 2017 12:52
-
-
Save Reedef/652f6f97136a48a0e1b3256cf7ab5b35 to your computer and use it in GitHub Desktop.
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
// Learnd From @石濑 | |
// Fetch Data | |
const request = new Promise(function(resolve, reject) { | |
fetch( | |
sth, | |
success => resolve('data'), | |
fail=> reject('reason') | |
); | |
}) | |
// Apply Some Universal Func Before Return To User | |
function applyPresets (res, dataPresets, errorPresets) { | |
const preseted = dataPresets.reduce((result, preset) => Promise.resolve(result).then(preset), res); | |
return errorPresets.reduce((result, preset) => Promise.resolve(result).catch(preset), preseted); | |
} | |
// Purely Fetch Data From Server | |
export function requestMTop ({ api, version = '1.0', data = {}}) { | |
return new Promise((resolve, reject) => | |
request({ | |
api, | |
version, | |
data | |
}, res => resolve(res), error => reject(error)) | |
); | |
} | |
// Pack request with presets | |
export default function requestDataWithPresets (MTopParams, dataPresets = [], errorPresets = []) { | |
return applyPresets(requestMTop(MTopParams), dataPresets, errorPresets); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment