Skip to content

Instantly share code, notes, and snippets.

@HACK21 HACK21/request.js
Created May 24, 2017

Embed
What would you like to do?
// 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
You can’t perform that action at this time.